This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17769|Reply: 0

[Communication] Several methods of the String class in C# (IndexOf, LastIndexOf, Substring)

[Copy link]
Posted on 1/31/2019 4:53:36 PM | | |
String.IndexOfString.IndexOf method (Char, Int32, Int32)
Reports the index of the first match for the specified character in this instance. The search starts with the specified character position and checks the specified number of character positions.
String.IndexOf(value, startIndex, count)

parameter
value: The Unicode character to look for.
startIndex: Search for the starting location.
count: The number of character positions to check.
Return value (Int32):
If the character is found, it is the index position of the value; Otherwise, if not found, it is -1.


Example:
string str = "Shenzhen Yingji Industrial Co., Ltd. International Tong Deng Shiwen * Shenzhen Yingji Industrial Co., Ltd. International Tong Deng Shiwen";
Label1.Text = str. IndexOf("China"). ToString(); Returns -1
Label1.Text = str. IndexOf("Yingji"). ToString(); Back to 3
Label1.Text = str. IndexOf("Yingji", 10). ToString(); Back to 21 Note: This is from the 10th character.
Label1.Text = str. IndexOf("Deng", 15, 10). ToString(); Returns -1
Label1.Text = str. IndexOf("Deng", 15, 20). ToString(); Return -32 Description: Start the search from the 15th character, the range to look is from the 15th character and then 20 characters, i.e. from the 15th-35th character.
String.LastIndexOfString.LastIndexOf method
Reports the index position of the last match of the specified Unicode character or String in this instance.

nameillustrate
String.LastIndexOf (Char)The report specifies the index location of the last match for the Unicode character in this instance.
String.LastIndexOf (String)Reports the index position of the last match within this instance of the specified String.
String.LastIndexOf (Char, Int32)The report specifies the index location of the last match for the Unicode character in this instance. The search starts with the specified character position.
String.LastIndexOf (String, Int32)Reports the index position of the last match within this instance of the specified String. The search starts with the specified character position.
String.LastIndexOf (String, StringComparison)Reports the index of the last match of the specified string in the current String object. A parameter specifies the type of search to be used to specify the string.
String.LastIndexOf (Char, Int32, Int32)Reports the index position of the last match in the substring of the specified Unicode character within this instance. The search starts with the specified character position and checks the specified number of character positions.
String.LastIndexOf (String, Int32, Int32)Reports the index position of the last match within this instance of the specified String. The search starts with the specified character position and checks the specified number of character positions.
String.LastIndexOf (String, Int32, StringComparison)Reports the index of the last match of the specified string in the current String object. The parameter specifies the starting search location in the current string, and the type of search to use to specify the string.
String.LastIndexOf (String, Int32, Int32, StringComparison)Reports the index position of the last match within this instance of the specified String object. The parameters specify the starting search location in the current string, the number of characters in the current string to be searched, and the type of search to use to specify the string.



Example:
string str = "Shenzhen Yingji Industrial Co., Ltd. International Tong Deng Shiwen * Shenzhen Yingji Industrial Co., Ltd. International Tong Deng Shiwen";
Label1.Text = str. LastIndexOf("Deng Wen"). ToString(); Back to -1
Label1.Text = str. LastIndexOf("Deng"). ToString(); Back to 32

Label1.Text = str. LastIndexOf("Deng", 8). ToString(); Back to -1
Label1.Text = str. LastIndexOf("Deng", 20). ToString(); Back to 14
Label1.Text = str. LastIndexOf("Deng", 33). ToString(); Back to 32
Description: Find characters in the specified range, this range is the parameter of the above input, which is understood to find the position of the last matching string from index 0 to the specified numerical position range. In the example, there is no "Deng" in 0-8, so it returns -1, in the 0-20 range, there is a "Deng" word on the index 14 position, and in the 0-33 range, there are two "Deng" words, because LastIndexOf returns the last match index position, so it returns 32 instead of 14.String.SubstringString.Substring method
Retrieve the substring from this instance.
nameillustrate
String.Substring (Int32)Retrieve the substring from this instance. The substring starts at the specified character position.
String.Substring (Int32, Int32)Retrieve the substring from this instance. The substring starts at the specified character position and has a specified length.



Example:
string str = "Shenzhen Yingji Industrial Co., Ltd. International Tong Deng Shiwen * Shenzhen Yingji Industrial Co., Ltd. International Tong Deng Shiwen";
Label1.Text = str. Substring(11); Back to "International Communication Deng Shiwen * Shenzhen Yingji Industrial Co., Ltd. International Communication Deng Shiwen"
Label1.Text = str. Substring(11,7); Back to "International Communications*"

To sum up:IndexOf and LastIndexOf both return a position and are integer values; If you can't find it, it will return -1;
IndexOf is searched from left to right, LastIndexOf is searched from right to left, whether it is IndexOf or LastIndexOf, the index sequence is from left to right (starting value is 0)
Substring is a string truncation, and the return value is a truncated string.




Previous:C# Remove string prefix specified characters String.Trim()
Next:Slash vs. backslash on Windows and Linux
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com