[Reserved] C # IndexOf method judged by whether a string is contained in another string

C # development process for a string String type of operation is a common operation, and sometimes need to determine whether a string is contained in another string, then you can use the IndexOf method and Contain method to achieve this function, and Contain method returns True false, and the method may IndexOf returns the index position is located, if the corresponding character string is found, it returns -1.

IndexOf method of using the format string in C #: StringA.IndexOf (StringB). StringA represent the search string, StringB it means you need to find the target string. If there is an index start location is returned, otherwise -1.

For example, the string StringA = "Hello World", the index began to look for the string "llo" is located in StringA position, using the following method

string StringA=“Hello World”;

var index = StringA.IndexOf ( "llo" );
the above statement returns a value of 2, is determined by its index is greater than 0 to whether the string contains.

If simply determines whether the string contains, may also be determined by the function Contain, as a function of the format Contain: StringA.Contain (StringB). Used to find a string representing StringA representative StringB contains the string of judgment. Examples of the above can be rewritten as:

bool isContain = StringA.Contain ( "llo" );
return result is True, i.e. comprising.

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link for the C # IndexOf method judged by whether a string contains _IT technology small fun house in another string .

Guess you like

Origin blog.csdn.net/weixin_39650424/article/details/93380487