次の例の例のように、パターンを照合するために正規表現を使用しています。母音を数えるために文字列を照合しています。
void VowelsCountInEachWord()
{
Regex rx = new Regex("[aeiou]");
var words=new string[]
{"aesthetic", "benevolent", "abstract",
"capricious", "complacent", "conciliatory",
"devious", "diligent", "discernible","dogmatic",
"eccentric","fallacious","indifferent","inquisitive",
"meticulous","pertinent","plausible", "reticent"
};
var filter = from w in words where (rx.IsMatch(w.ToLower())) select new
{w,count=VowelsCounting(w)};
foreach (var v in filter)
{
Console.WriteLine("String {0} contains {1} vowels", v.w, v.count);
}
}
public int VowelsCounting(string value)
{
int cnt=0;
foreach (char c in value)
{
switch (c)
{
case 'a':cnt++;break;
case 'e':cnt++;break;
case 'i':cnt++;break;
case 'o':cnt++;break;
case 'u':cnt++;break;
}
}
return cnt++;
}
1)正規表現を使用せずに、C#はパターンを一致させるための構成を提供しますか?
2)文字列に対して個々の文字をカウントするには、独自のメソッドを導出する必要がありますか?