Reputation: 9043
I have a char list of which I want to convert the vowels of this list to uppercase.
With the way I am doing it only returns the vowels and excludes all the consonants.
I know this is very elementary but how would I do this correctly.?
List<char> lstVowels = new List<char>() {'a', 'e', 'i', 'o', 'u' };
lstChar = lstChar.Where(p=>lstVowels.Contains(p)).Select(t => char.ToUpper(t)).ToList();
//lstChar contains a set of consonants and vowels
Upvotes: 3
Views: 1043
Reputation: 66449
If a character exists in lstVowels
then convert it to uppercase; otherwise, just keep it as-is.
lstChar =
lstChar.Select(c => lstVowels.Contains(c) ? char.ToUpper(c) : c).ToList();
In your code, the Where
clause causes you to lose all characters that don't exist in lstVowels
.
Upvotes: 6
Reputation: 43300
personally I'd include an ternary if into your select instead
lstChar.Select(c => lstVowels.Contains(c) ? char.ToUpper(c) : c).ToList();
Upvotes: 3
Reputation: 59655
That's because you are filtering out consonants instead of just keeping them unchanged.
lstChar.Select(c => lstVowels.Contains(c) ? Char.ToUpper(c) : c)
Upvotes: 1