Để kiểm tra tất cả các nguyên âm, trước tiên hãy đặt điều kiện để kiểm tra -
string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();
Ở trên, chúng tôi đã sử dụng chuỗi -
string str = "the quick brown fox jumps over the lazy dog";
Bây giờ, sử dụng phương thức Any () sẽ kiểm tra xem chuỗi có bất kỳ nguyên âm nào hay không -
if(!res.Any())
Console.WriteLine("No vowels!"); Lặp qua chuỗi để lấy các nguyên âm -
Ví dụ
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);
}
} Đầu ra
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