The usage of IndexOf, LastIndexOf and substring

Source: Internet
Author: User
Tags truncated

Today encountered the problem of intercepting strings, in the online search indexof, LastIndexOf, substring these three kinds of interception of the use of the string is summarized as follows:

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.

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 *"

Label1.Text = str. Substring (str. length-3,3); Return to Deng's text , which is the 3-character truncated

Summary:

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.

The usage of IndexOf, LastIndexOf and substring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.