How to check if a string contains a certain word in C#?

CsharpProgrammingServer Side Programming

Use the Contains() method to check if a string contains a word or not.

Set the string −

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

Now let’s say you need to find the word “much”

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

Let us see the complete code −

Example

 Live Demo

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!");
      }
   }
}

Output

Word found!
raja
Published on 20-Aug-2018 13:25:23
Advertisements