Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a string exists in another string

Tags:

c#

Hope somebody can help me. I am just learning C# and I have a simple question.

I have a variable and I would like to check if that exists in another string. Something like

if ( test contains "abc" ) {  } 

Is there an easy way to do this in C#

like image 971
Janet Avatar asked May 01 '11 12:05

Janet


People also ask

How do you check if a string is present in another string in Python?

The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

How do you check if a string is a part of another string in Javascript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do you check if a string is substring of another string in C++?

Check if a string contains a sub-string in C++ This find() method returns the first location where the string is found. Here we are using this find() function multiple times to get all of the matches. If the item is found, this function returns the position. But if it is not found, it will return string::npos.

How do you check if a character in a string exists?

Use the String. indexOf() method to check if a string contains a character, e.g. if (str. indexOf(char) !== -1) {} .


2 Answers

Use String.Contains:

if (stringValue.Contains(anotherStringValue)) {       // Do Something //  } 
like image 130
Akram Shahda Avatar answered Oct 14 '22 03:10

Akram Shahda


IndexOf() function will do the work...
It will return -1 if the string does not exist

like image 30
liron Avatar answered Oct 14 '22 01:10

liron