如何在 C# 中檢查字串中是否包含某個單詞?


使用 Contains() 方法檢查字串是否包含某個單詞。

設定字串 −

string s = "Together we can do so much!";

現在假設你需要查詢單詞“much”

if (s.Contains("much") == true) {
   Console.WriteLine("Word found!");
}

讓我們看看完整的程式碼 −

示例

 現場演示

using System;

public class Demo {
   public static void Main() {
      string s = "Together we can do so much!";
      if (s.Contains("much") == true) {
         Console.WriteLine("Word found!");
      } else {
         Console.WriteLine("Word not found!");
      }
   }
}

輸出

Word found!

更新於: 2020 年 6 月 22 日

16K+ 觀看

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.