How to search Strings Using String Methods in C#

The following example uses the IndexOf, LastIndexOf, StartsWith, and EndsWith methods to search the strings.

string str = "Welcome to Author Code";
 // If you want to find that string str starts with 'Welcome' or not than you can use StartsWith(string) method like as:
 //Simple comparisons are always case sensitive!
 bool isStartWith1 = str.StartsWith("welcome");
if (isStartWith1)
       MessageBox.Show("string starts with 'Welcome' is true");
       MessageBox.Show("string starts with 'Welcome' is false");
// use the StringComparison parameter on methods that have it to specify how to match strings.
bool isStartWith2 = str.StartsWith("welcome", System.StringComparison.CurrentCultureIgnoreCase);
bool isEndWith1 = str.EndsWith("Code", System.StringComparison.CurrentCultureIgnoreCase);
 //You can find substring between two string, suppose you want to find substring between 'Welcome' and 'Code',simply you can do this like:
int firstStr = str.IndexOf("Welcome") + "methods".Length;
int lastStr = str.LastIndexOf("Code");
string subStr = str.Substring(firstStr, lastStr - firstStr);
MessageBox.Show("substring between 'Welcome' and 'Code' is " + subStr);