[英]Simplest way of checking vowels in a list of string
I have the code 我有代码
List<String> words
There is only currently 2 counted words in the list which are: [0] "abc", [1] "abc" 列表中目前只有2个计数字:[0]“ abc”,[1]“ abc”
What is the simplest way of going through the list and checking if a vowel exists in these words? 检查列表并检查这些单词中是否存在元音的最简单方法是什么?
You can use LINQ to find the list of all words with vowels in it. 您可以使用LINQ查找其中带有元音的所有单词的列表。
string vowels = "aeiou";
List<string> words = new List<string>() { "apple", "crypt" };
List<string> wordsWithVowels = words.Where(w => w.ToArray().Intersect(vowels.ToArray()).Count() > 0).ToList();
OR 要么
List<string> wordsWithVowels = words.Where(w => w.ToArray().Any(c => vowels.ToArray().Contains(Char.ToLower(c)))).ToList();
Use Char.ToLower for case sensitivity 使用Char.ToLower区分大小写
public class VowelChecker
{
private char[] vowels = new [] { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' };
public bool ContainsVowel(string word)
{
foreach (char vowel in vowels)
{
if (word.Contains(vowel))
{
return true;
}
}
return false;
}
}
Usage: 用法:
List<string> words = new List<string>();
words.Add("ABC");
words.Add("elephant");
words.Add("FLY");
VowelChecker checker = new VowelChecker();
foreach (string word in words)
{
Console.WriteLine("{0} contains a vowel: {1}", word, checker.ContainsVowel(word));
}
Console.ReadLine();
Results: 结果:
This problem is quite easy to solve in various ways... The only thing that is important is to know that not everywhere the lower-to-upper case conversion is the same... See http://blog.codinghorror.com/whats-wrong-with-turkey/ (starting from Strings are where it really starts to get hairy ) 这个问题很容易以各种方式解决...唯一重要的是要知道并非所有地方的小写到小写转换都是相同的...请参阅http://blog.codinghorror.com/ whats-wrong-with-turkey / (从字符串开始才真正开始变得毛茸茸 )
To make it short, in Turkey the uppercase of i
is İ
, while the lowercase of I
is ı
. 简而言之,在土耳其, i
的大写字母是İ
,而I
的小写字母是ı
。 see the difference? 看到不同? :-) :-)
string vowels = "aeiou";
List<string> words = new List<string>() { "APPLE", "crypt" };
List<string> wordsWithVowels = words.Where(w => vowels.Any(v => w.IndexOf(v.ToString(), StringComparison.CurrentCultureIgnoreCase) != -1)).ToList();
or probably even more pedantic-correct 甚至可能是更矫正的
CompareInfo comparer = new CultureInfo("en-US").CompareInfo;
List<string> wordsWithVowels = words.Where(w => vowels.Any(v => comparer.IndexOf(w, v, CompareOptions.IgnoreCase) != -1)).ToList();
(where en-US is the language you are looking the vowels of) (其中en-US是您正在寻找的元音的语言)
There are very good methods to do case insensitive comparisons, and so it is useless and wrong to use ToLower()
/ ToUpper()
to do them. 有很多很好的方法来进行不区分大小写的比较,因此使用ToLower()
/ ToUpper()
来进行它们是没有用和错误的。
As I've commented, this is probably moot because the concept of vowel is language-connected, so in Turkey they have different vowels. 正如我评论过的那样,这可能是没有意义的,因为元音的概念与语言相关,因此在土耳其,它们具有不同的元音。
Note that even in English, sometimes the y
is a vowel (see wiki ) 请注意,即使是英语,有时y
也是一个元音(请参阅wiki )
This feels like homework but I'm bored. 感觉就像是作业,但我很无聊。
public int CountVowels(List<string> words)
{
return words.Sum(CountVowels);
}
public int CountVowels(string word)
{
return Enumerable.Range(0, word.Length).Count(i => IsVowel(word, i));
}
public bool IsVowel(string word, int i)
{
if (i < 0) return false;
if (i >= word.length) return false;
switch(word[i].ToLowerCase())
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
case 'y':
// I'm pretty sure 'y' is a consonant if it follows,
// or is followed by a vowel
return !(IsVowel(word, i-1) || IsVowel(word, i+1));
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.