如何在 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!
廣告