ในการตรวจสอบสระทั้งหมด ก่อนอื่นให้ตั้งค่าเงื่อนไขเพื่อตรวจสอบ -
string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();
ด้านบนเราใช้สตริง −
string str = "the quick brown fox jumps over the lazy dog";
ตอนนี้ ใช้เมธอด Any() ตรวจสอบว่าสตริงมีสระหรือไม่ -
if(!res.Any())
Console.WriteLine("No vowels!"); วนผ่านสตริงเพื่อให้ได้สระ -
ตัวอย่าง
using System;
using System.Linq;
public class Program {
public static void Main() {
string str = "the quick brown fox jumps over the lazy dog";
var res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();
if(!res.Any())
Console.WriteLine("No vowels!");
foreach(var vowel in res)
Console.WriteLine("Your phrase contains vowel = {0}", vowel);
}
} ผลลัพธ์
Your phrase contains vowel = e Your phrase contains vowel = u Your phrase contains vowel = i Your phrase contains vowel = o Your phrase contains vowel = a