c# check if string contains character code example

Example 1: how to cjeck if a string has a word c#

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

Example 2: c# string contains

bool b = s1.Contains("myString");

Example 3: check if string is in string[] c#

string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
    if (x == stringToCheck)
    {
        // Process...
    }
}

Example 4: c# check if string contains value

string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b = s1.Contains(s2);
Console.WriteLine("'{0}' is in the string '{1}': {2}",
                s2, s1, b);
if (b) {
    int index = s1.IndexOf(s2);
    if (index >= 0)
        Console.WriteLine("'{0} begins at character position {1}",
                      s2, index + 1);
}
// This example displays the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17

Example 5: check if string is in string[] c#

Using System.Linq;


if(stringArray.All(stringToCheck.Contains)){
	//Process
}

Tags:

Lua Example