Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, November 21, 2016 2:16 PM
Hi
So Im new to C# and Im trying to figur it out but i have a problem. I want to compare two char arrays one is a string that I converted to an array (textInputTecken) and the other one is a char array containing vowels (vokaler). I want to change all the vowels in the textInputTecken to the letter i, I have another way of doing without the char array with the vowels (vokaler).
for (int i = 0; i < textInputTecken.Length; i++)
{
if (textInputTecken[i] == 'a' || textInputTecken[i] == 'e' || textInputTecken[i] == 'i' || textInputTecken[i] == 'o' || textInputTecken[i] == 'u' || textInputTecken[i] == 'y' || textInputTecken[i] == 'å' || textInputTecken[i] == 'ä' || textInputTecken[i] == 'ö' || textInputTecken[i] == 'A' || textInputTecken[i] == 'E' || textInputTecken[i] == 'I' || textInputTecken[i] == 'O' || textInputTecken[i] == 'U' || textInputTecken[i] == 'Y' || textInputTecken[i] == 'Å' || textInputTecken[i] == 'Ä' || textInputTecken[i] == 'Ö')
{
nya = textInput.Replace("a", "i").Replace("e", "i").Replace("o", "i").Replace("u", "i").Replace("y", "i").Replace("å", "i").Replace("ä", "i").Replace("ö", "i").Replace("A", "i").Replace("E", "i").Replace("I", "i").Replace("O", "i").Replace("U", "i").Replace("Y", "i").Replace("Å", "i").Replace("Ä", "i").Replace("Ö", "i");
txtConvert.Text = nya;
total++;
lblVokalAntal.Text = total.ToString() + " Vokaler";
}
}
I have tried doing this but it doesnt work. How can I get this to work?
char[] vokaler = new char[] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y', 'å', 'Å', 'ä', 'Ä', 'ö', 'Ö' };
for (int i = 0; i < textInputTecken.Length; i++)
{
if (textInputTecken[i] == vokaler[i])
{
nya = textInput.Replace("a", "i").Replace("e", "i").Replace("o", "i").Replace("u", "i").Replace("y", "i").Replace("å", "i").Replace("ä", "i").Replace("ö", "i").Replace("A", "i").Replace("E", "i").Replace("I", "i").Replace("O", "i").Replace("U", "i").Replace("Y", "i").Replace("Å", "i").Replace("Ä", "i").Replace("Ö", "i");
txtConvert.Text = nya;
total++;
lblVokalAntal.Text = total.ToString() + " Vokaler";
}
}
All replies (4)
Monday, November 21, 2016 3:19 PM ✅Answered
Or using Replace():
namespace ConsoleCS { using System; public class Program { static void Main(string[] args) { char[] vokaler = new char[] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y', 'å', 'Å', 'ä', 'Ä', 'ö', 'Ö' }; char[] textInputTecken = new char[] { 'a', 'b', 'c', 'd', 'e' }; string dummy = new string(textInputTecken); for (int v = 0; v < vokaler.Length; v++) { dummy= dummy.Replace(vokaler[v], 'X'); } textInputTecken = dummy.ToCharArray(); Console.WriteLine("Result is {0}.", new string(textInputTecken)); Console.WriteLine("Done."); Console.ReadLine(); } } }
Some minor additions:
- It is not nessesary to translate a String into a char[]. Indexers in .NET are class scope functions and string has them. You can use indexers on the string itself. The other way around however is nessesary. Of course String does have a constructor that takes a char[] as input, so also easy.
- You are using non-english characters. As such, normalisation might be an issue when doing this. It might be a issues as far back as creating the Array from a string. Make certain you do that part properly or you run into issues:
https://msdn.microsoft.com/en-us/library/8eaxk1x2.aspx
Remember to mark helpfull answers as helpfull and close threads by marking answers.
Monday, November 21, 2016 2:42 PM | 1 vote
As you're new: post concise and complete examples. This means craft a console application which shows this behavior and post the entire code. Using a console application also reduces the complexity to a minimum which is also good for learning purposes.
Cause fore example in your snippets the important declaration of textInputTecken is missing. Also it seems that you may run in boundary problems.
E.g.
namespace ConsoleCS
{
using System;
public class Program
{
static void Main(string[] args)
{
char[] vokaler = new char[] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y', 'å', 'Å', 'ä', 'Ä', 'ö', 'Ö' };
char[] textInputTecken = new char[] { 'a', 'b', 'c', 'd', 'e' };
for (int v = 0; v < vokaler.Length; v++)
{
for (int i = 0; i < textInputTecken.Length; i++)
{
if (textInputTecken[i] == vokaler[v])
{
Console.WriteLine("Match at position {0} in textInputTecken with position {1} in vokaler ({2})", i, v,vokaler[v]);
textInputTecken[i] = 'X';
}
}
}
Console.WriteLine("Result is {0}.", new string(textInputTecken));
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
Also your using some more undefined symbols..
Monday, November 21, 2016 2:56 PM
Or using Replace():
namespace ConsoleCS
{
using System;
public class Program
{
static void Main(string[] args)
{
char[] vokaler = new char[] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y', 'å', 'Å', 'ä', 'Ä', 'ö', 'Ö' };
char[] textInputTecken = new char[] { 'a', 'b', 'c', 'd', 'e' };
string dummy = new string(textInputTecken);
for (int v = 0; v < vokaler.Length; v++)
{
dummy= dummy.Replace(vokaler[v], 'X');
}
textInputTecken = dummy.ToCharArray();
Console.WriteLine("Result is {0}.", new string(textInputTecken));
Console.WriteLine("Done.");
Console.ReadLine();
}
}
}
Monday, November 21, 2016 5:53 PM
Why is that the answer??