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


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!

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements