C# 程式檢查字串中是否包含所有母音


要檢查所有母音,首先設定要檢查的條件 -

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

更新日期:2020-06-22

435 瀏覽量

開 啟您的 職業生涯

完成課程,獲取認證

開始
廣告
© . All rights reserved.