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