c# search string code example

Example 1: c sharp index of substring

// To find the index of the first substring in a string use
// 'IndexOf()'
string str = "Hello World!"
str.IndexOf("o"); // Output: 4

// You can give a starting index and a length to search at
str.IndexOf("o", 6, 4); // Output: 7

// To find the last instance of a substring use 'LastIndexOf()'
str.LastIndexOf("o"); // Output: 7

Example 2: how to look for substring in string in c#

String phrase = "this is the ultimate string";

Console.WriteLine(phrase.Contains("this")); //returns True
Console.WriteLine(phrase.Contains("python")); //returns False

Example 3: search letters in list c#

The characters of the string in reverse are : 

m  o  c  .  e  c  r  u  o  s  e  r  3  w

Example 4: search letters in list c#

Total number of words in the string is : 3