EN
C# / .NET - string contains
11
points
In C# / .NET it is possible to check string existence in following ways.
1. String.Contains
method example
string text = "My name is John.";
if (text.Contains("John"))
Console.WriteLine("Text contains John name.");
Output:
Text contains John name.
2. String.IndexOf
method example
string text = "My name is John.";
int index = text.IndexOf("John");
if (index != -1)
Console.WriteLine("Text contans John name.");
Output:
Text contains John name.
3. String.LastIndexOf
method example
string text = "My name is John.";
int index = text.LastIndexOf("John");
if (index != -1)
Console.WriteLine("Text contans John name.");
Output:
Text contains John name.
4. Regex.IsMatch
method example
string text = "My name is John.";
Regex regex = new Regex("John");
if (regex.IsMatch(text))
Console.WriteLine("Text contans John name.");
Output:
Text contains John name.