String.IndexOf
String.IndexOf method (Char, Int32, Int32)
Reports the index of the first occurrence of a specified character in this instance. The search starts at the specified character position and checks the specified number of character positions.
String.IndexOf (value, StartIndex, Count)
Parameters
Value: The Unicode character to find.
StartIndex: Search start location.
Count: The number of character positions to check.
return value (INT32):
The index position of value if the character is found, or 1 if not found.
Example:
String str = "Shenzhen Industrial Co., Ltd. International Tong Deng Wen * Shenzhen Ying Ji Industrial Co., Ltd."
Label1.Text = str. IndexOf ("China"). ToString ();//Return-1
Label1.Text = str. IndexOf ("Ying Ji"). ToString ();//Return 3
Label1.Text = str. IndexOf ("Ying Ji", 10). ToString ();//Returns 21 Description: This is the beginning of the 10th character search.
Label1.Text = str. IndexOf ("Deng", 15,10). ToString ();//Return-1
Label1.Text = str. IndexOf ("Deng", 15,20). ToString ();//Return-32 Description: Starting with the 15th character, the range to look for is 20 characters from the beginning of the 15th character, which is the search from the 第15-35个 character.
String.LastIndexOf
String.LastIndexOf method
Reports the index position of the last occurrence of the specified Unicode character or String in this instance.
Name |
Description |
|
String.LastIndexOf (Char) |
Reports the index position of the last occurrence of the specified Unicode character in this instance. |
String.LastIndexOf (String) |
Reports the index position of the last occurrence of the specified String within this instance. |
String.LastIndexOf (Char, Int32) |
Reports the index position of the last occurrence of the specified Unicode character in this instance. The search starts at the specified character position. |
String.LastIndexOf (String, Int32) |
Reports the index position of the last occurrence of the specified String within this instance. The search starts at the specified character position. |
String.LastIndexOf (String, StringComparison) |
Reports the index of the last occurrence of the specified string in the current string object. A parameter that specifies the type of search to use for the specified string. |
String.LastIndexOf (Char, Int32, Int32) |
Reports the index position of the last occurrence of the specified Unicode character in a substring within this instance. The search starts at the specified character position and checks the specified number of character positions. |
String.LastIndexOf (String, Int32, Int32) |
Reports the index position of the last occurrence of the specified String within this instance. The search starts at the specified character position and checks the specified number of character positions. |
String.LastIndexOf (String, Int32, StringComparison) |
Reports the index of the last occurrence of the specified string in the current string object. parameter specifies the starting search location in the current string, and the type of search to use for the specified string. |
String.LastIndexOf (String, Int32, Int32, StringComparison) |
Reports the index position of the last occurrence of the specified String object within this instance. parameter specifies the starting search location in the current string, the number of characters in the current string to search for, and the type of search to use for the specified string. |
Example:
String str = "Shenzhen Industrial Co., Ltd. International Tong Deng Wen * Shenzhen Ying Ji Industrial Co., Ltd."
Label1.Text = str. LastIndexOf ("Deng Wen"). ToString ();//Return-1
Label1.Text = str. LastIndexOf ("Deng"). ToString ();//Return 32
Label1.Text = str. LastIndexOf ("Deng", 8). ToString ();//Return-1
Label1.Text = str. LastIndexOf ("Deng", 20). ToString ();//Return 14
Label1.Text = str. LastIndexOf ("Deng", 33). ToString ();//Return 32
Description: Finds the character in the specified range, which is the input parameter above, and is understood to be the position of the last matched string from the beginning of index 0 to the specified numeric position range. In the example, 0-8 does not have the word "Deng", so return to the -1,0-20 range, there is a "Deng" in the index 14 position, 0-33 in the range of two "Deng" word, because LastIndexOf is to return the last match index position, so return 32, instead of 14.
String.substring
String.substring method
Retrieves a substring from this instance.
Name |
Description |
String.substring (Int32) |
Retrieves a substring from this instance. The substring starts at the specified character position. |
String.substring (Int32, Int32) |
Retrieves a substring from this instance. The substring starts at the specified character position and has the specified length. |
Example:
String str = "Shenzhen Industrial Co., Ltd. International Tong Deng Wen * Shenzhen Ying Ji Industrial Co., Ltd."
Label1.Text = str. Substring (11);//Return to "International Tong Deng Wen * Shenzhen Industrial Co., Ltd."
Label1.Text = str. Substring (11,7);//Return to "International Tong Tang Wen *"
To summarize:
IndexOf, LastIndexOf all return a position, is an integer value; 1 is not found;
IndexOf is from left to right, LastIndexOf is right-to-left, whether it is indexof or LastIndexOf, the index sequence is left to right (the starting value is 0)
SUBSTRING is a string intercept, and the return value is a truncated string.
Several methods of the String class in C # (IndexOf, LastIndexOf, Substring)