0

I'm Learning c# and i am making some exercises. i was asked to make a program that make an array of strings and remove the vowels form it's words i did this code to remove the vowel "S" but it didn't work. can someone help me with that ?

string[] musicinst = new string[4] { "cello", "guitar", "violin", "double bass" };

foreach (string s in musicinst)
{
    if (s.Contains("s")) { s.Replace("s", ""); }
    Console.WriteLine(s);                                       
}

now this code outputs the words exactly as i typed them in the array with no changes. so what is the problem here ?

4
  • You have to define a new string...Replace returns a string, it doesn't modify the original Commented Oct 23, 2016 at 13:44
  • 3
    Where I come from, 'S' is not a vowel but a consonant. Just saying ;) Commented Oct 23, 2016 at 13:47
  • @BitTickler just for example, i didn't think about which vowel :"D Commented Oct 23, 2016 at 13:48
  • Check this, nice solution using Regex - stackoverflow.com/questions/7265315/… Commented Oct 23, 2016 at 14:42

4 Answers 4

4

.Replace does not change the string but returns a new string with the change. You need to now assign it back to s:

if (s.Contains("s")) 
{
    s = s.Replace("s", "o"); 
}

This will now also not work:

Cannot assign to 's' because it is a 'foreach iteration variable'

So instead use a for loop and access by indexer or create a new list and add the result of s.Replace to it:

string[] musicinst = new string[4] { "cello", "guitar", "violin", "double bass" };
var newData = musicinst.Select(item => item.Replace("s", "o")).ToArray();

If you need to deal with replacement when insensitive then look at: Is there an alternative to string.Replace that is case-insensitive?

Sign up to request clarification or add additional context in comments.

Comments

3

You're running into a feature of C# strings called immutability - operations on strings do not change the string, it returns a new string. given this, you might think you need to do this:

s = s.Replace("s", "o");

But that won't work because 's' is a foreach iterator. Your best bet is to recast your loop:

for (int i = 0; i < musicinst.Length; ++i)
{
    if (musicinst[i].Contains("s"))
    {
        musicinst[i] = musicinst.Replace("s", "o");
    }
}

Which will change your array in-place. To preserve immutability of the array as well you might consider a LINQ-like option that builds a new array as others have demonstrated.

Comments

2
    static void Main(string[] args)
    {
        string[] musicinst = new string[4] { "cello", "guitar", "violin", "double bass" };
        char[] vowels = new char[5] { 'a', 'e', 'i' ,'o', 'u' };

        List<string> output = new List<string>();
        foreach (string s in musicinst)
        {
            string s1 = s;
            foreach (var v in vowels)
            {
                if (s1.Contains(v))
                {
                  s1=s1.Remove(s1.IndexOf(v),1);
                }
            }
            output.Add(s1);
        }

        Console.ReadLine();

    }

2 Comments

Try this code. Since strings are immutable, you have to make a output variable where you can store your strings without vowels.
Good solution, preferably use extension method of Contains, which is Case insensitive, +1
0

Whenever you perform any operation on a string data type, it creates a new string which you have to store in a new variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.