如何在 C# 中检查字符串是否包含某个单词?

csharpprogrammingserver side programming更新于 2025/5/26 15:52:17

使用 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!

相关文章