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!
Updated on: 2020-06-22T09:12:21+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements