user10503656
user10503656

Reputation:

C# Replace All Vowels in Generic List

How do I replace all vowels in list with a space? Following code does not seem to be working.

        List<string> Instruments = new List<string>();

        Instruments.Add("cello");
        Instruments.Add("guitar");
        Instruments.Add("violin");
        Instruments.Add("double bass");

        string vowels = "a e i o u y";


        Instruments.ForEach(w=>vowels = vowels.Replace(w,""));

Expected Result:

cll
gtr
vln

Upvotes: 0

Views: 1734

Answers (6)

Enigmativity
Enigmativity

Reputation: 117064

This works nicely for me:

List<string> Instruments = new List<string>();

Instruments.Add("cello");
Instruments.Add("guitar");
Instruments.Add("violin");
Instruments.Add("double bass");

string vowels = "aeiouy";

var results = vowels.Aggregate(Instruments,
    (i, v) => i.Select(x => x.Replace(v.ToString(), "")).ToList());

I get:

cll 
gtr 
vln 
dbl bss 

Upvotes: 0

Brett Caswell
Brett Caswell

Reputation: 1504

Instruments has a collection of string, or words, in it. Your .ForEach iterates through that collection where each instance is w, but you're not affecting those instances with your usage of .Replace here, you're using them in an operation to affect the string vowels.

see String.Replace MSN Documentation.

As such, you need to also iterate through your vowels string, and use the charinstance of vowels: w.Replace(v, " "), where w is your word instance in Instruments, and v is your vowel instance in vowels:

So, it should be Instruments.ForEach(w => foreach (vowelChar in vowels.split(" ")) { w.Replace(vowelChar, " "); });

Note: @mjwills pointed out the other issue with this operation in comments. the assignment to w won't persist here. so, you'll need to create new List<string> in some fashion to persist it (either declare it before hand and add to it in iteration, or use Linq .ToList<T> Extension of IEnumerable<T>.


However, that is inefficient since you're essentially creating a char[] from vowels string using .Split on each iteration of Instruments.

Instead, you should defined your vowels as a char[] to avoid the necessity of that operation: var vowels = new char[] {'a','e','i','o','u', 'y'};

    List<string> Instruments = new List<string>();

    Instruments.Add("cello");
    Instruments.Add("guitar");
    Instruments.Add("violin");
    Instruments.Add("double bass");

    char[] vowels = new char[] {'a','e','i','o','u','y'};

    Instruments.ForEach(w => {
        foreach (char v in vowels) {
           w = w.Replace(v, ' ');
        }
    });

EDIT: assigning to w instances of Instruments in the iteration don't persist, so you would need to create a new instance of List<string> for your results.

    List<string> results = new List<string>();
    Instruments.ForEach(w => {
        foreach (char v in vowels) {
           results.Add(w.Replace(v, ' '));
        }
    });

    results.ForEach(w => Console.WriteLine(w));

Upvotes: 0

JSteward
JSteward

Reputation: 7091

You could try the same with with Regex:

public void ReplaceAllVowels()
{
    List<string> Instruments = new List<string>();

    Instruments.Add("cello");
    Instruments.Add("guitar");
    Instruments.Add("violin");
    Instruments.Add("double bass");

    var pattern = new Regex("[aeiouy]");
    var lst = Instruments.Select(i => pattern.Replace(i, "")).ToList();
    foreach (var item in lst)
    {
        Console.WriteLine(item);
    }
}

Upvotes: 0

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

Try this

var vowels = new List<char> {'a','e','i','o','u','y'};

var result = new List<string>();
Instruments.ForEach(w => result.Add(new string(w.Select(x => vowels.Any(y => y == x) ? ' ' : x).ToArray())));

Upvotes: 2

Sean
Sean

Reputation: 1557

A little quick and dirty but this works for me.

  List<string> Instruments = new List<string>();
  var newList = new List<String>();

  Instruments.Add("cello");
  Instruments.Add("guitar");
  Instruments.Add("violin");
  Instruments.Add("double bass");

  List<string> vowels = new List<string> { "a", "e", "i", "o", "u", "y" };

  Instruments.ForEach(w =>
  {
    var temp = w;
    vowels.ForEach(v =>
    {
      temp = temp.Replace(v, "");
    });
    newList.Add(temp);
  });

  newList.ForEach(w => Console.WriteLine(w));

Upvotes: 0

Gauravsa
Gauravsa

Reputation: 6524

You should use .Select if need to changes values in Collection:

List<string> Instruments = new List<string>();

        Instruments.Add("cello");
        Instruments.Add("guitar");
        Instruments.Add("violin");
        Instruments.Add("double bass");

        var regex = new Regex("^a|e|i|o|u", RegexOptions.IgnoreCase);

        var withoutVowels = from instr in Instruments
            select regex.Replace(instr, string.Empty);

        foreach (var item in withoutVowels)
        {
            Console.WriteLine(item);
        }

Upvotes: 1

Related Questions