Methods of the string class in C # (indexof, lastindexof, substring)

Source: Internet
Author: User
Tags ranges
String. indexof

String. indexof method (char, int32, int32)
The index of the first matching item of the specified character in this instance. Searches start from the specified character location and checks the specified number of character locations.
String. indexof (value, startindex, count)

Parameters
Value: the Unicode character to be searched.
Startindex: Start position of the search.
Count: the number of character locations to check.
Return Value (int32 ):
If this character is found, it is the index position of the value; otherwise, 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 (); // return-1
Label1.text = Str. indexof (""). tostring (); // 3 is returned.
Label1.text = Str. indexof ("", 10). tostring (); // returns the value 21. Note: This is a query starting from 10th characters.
Label1.text = Str. indexof ("Deng", 15,10). tostring (); // return-1
Label1.text = Str. indexof ("Deng", 15, 20 ). tostring (); // return-32 Description: It starts from 15th characters and ranges from 15th to 20 characters, that is, search from the characters 15-35.

String. lastindexof

String. lastindexof Method
Report the index location of the last matching item of the specified UNICODE character or string in this instance.

Name Description: String. lastindexof (char) reports the index location of the last matching item of the specified UNICODE character in this instance. String. lastindexof (string) reports the index location of the last matched string in this instance. String. lastindexof (char, int32) reports the index location of the last matching item of the specified UNICODE character in this instance. The search starts with the specified character position. String. lastindexof (string, int32) reports the index location of the last matched string in this instance. The search starts with the specified character position. String. lastindexof (string, stringcomparison) reports the index of the last matched string in the current String object. A parameter specifies the search type of the string to be used. String. lastindexof (char, int32, int32) reports the index location of the last matched item in the substring in this instance. Searches start from the specified character location and checks the specified number of character locations. String. lastindexof (string, int32, int32) reports the index location of the last matched string in this instance. Searches start from the specified character location and checks the specified number of character locations. String. lastindexof (string, int32, stringcomparison) reports the index of the last matched string in the current String object. The parameter specifies the start position of the current string and the search type to be used to specify the string. String. lastindexof (string, int32, int32, stringcomparison) reports the index location of the last matching item of the specified String object in this instance. The parameter specifies the start position of the current string, the number of characters in the current string to be searched, and the search type to be used 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 (); // 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: searches for characters within the specified range. The range is the input parameter above, which is interpreted, searches for the position of the last matched string from index 0 to the specified value range. In the example, there is no "Deng" in 0-8. Therefore, in the range of--20, a "Deng" word is returned at the index position 14, there are two "Deng" characters in the range of 0 to 33, because lastindexof is the index location of the last matched item, so 32 is returned, instead of 14.

String. substring

String. substring Method
Retrieve the substring from this instance.

Name Description
String. substring (int32) Retrieve the substring from this instance. The substring starts from the specified character position.
String. substring (int32, int32) Retrieve the substring from this instance. The substring starts from the specified character position and has the 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); // return to "Deng shiwen of international communication * Deng shiwen of Shenzhen yingji Industrial Co., Ltd. of international communication"
Label1.text = Str. substring (); // The system returns the message "Deng shenwen of international communication *"

Summary:

Indexof and lastindexof both return an integer and an integer. If no value is found,-1 is returned;
Indexof is from left to right, lastindexof is from right to left, whether it is indexof or lastindexof, the index sequence is from left to right (the starting value is 0)
Substring is the string truncation, And the return value is the string after the truncation.

String. indexof

String. indexof method (char, int32, int32)
The index of the first matching item of the specified character in this instance. Searches start from the specified character location and checks the specified number of character locations.
String. indexof (value, startindex, count)

Parameters
Value: the Unicode character to be searched.
Startindex: Start position of the search.
Count: the number of character locations to check.
Return Value (int32 ):
If this character is found, it is the index position of the value; otherwise, 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 (); // return-1
Label1.text = Str. indexof (""). tostring (); // 3 is returned.
Label1.text = Str. indexof ("", 10). tostring (); // returns the value 21. Note: This is a query starting from 10th characters.
Label1.text = Str. indexof ("Deng", 15,10). tostring (); // return-1
Label1.text = Str. indexof ("Deng", 15, 20 ). tostring (); // return-32 Description: It starts from 15th characters and ranges from 15th to 20 characters, that is, search from the characters 15-35.

String. lastindexof

String. lastindexof Method
Report the index location of the last matching item of the specified UNICODE character or string in this instance.

Name Description: String. lastindexof (char) reports the index location of the last matching item of the specified UNICODE character in this instance. String. lastindexof (string) reports the index location of the last matched string in this instance. String. lastindexof (char, int32) reports the index location of the last matching item of the specified UNICODE character in this instance. The search starts with the specified character position. String. lastindexof (string, int32) reports the index location of the last matched string in this instance. The search starts with the specified character position. String. lastindexof (string, stringcomparison) reports the index of the last matched string in the current String object. A parameter specifies the search type of the string to be used. String. lastindexof (char, int32, int32) reports the index location of the last matched item in the substring in this instance. Searches start from the specified character location and checks the specified number of character locations. String. lastindexof (string, int32, int32) reports the index location of the last matched string in this instance. Searches start from the specified character location and checks the specified number of character locations. String. lastindexof (string, int32, stringcomparison) reports the index of the last matched string in the current String object. The parameter specifies the start position of the current string and the search type to be used to specify the string. String. lastindexof (string, int32, int32, stringcomparison) reports the index location of the last matching item of the specified String object in this instance. The parameter specifies the start position of the current string, the number of characters in the current string to be searched, and the search type to be used 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 (); // 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: searches for characters within the specified range. The range is the input parameter above, which is interpreted, searches for the position of the last matched string from index 0 to the specified value range. In the example, there is no "Deng" in 0-8. Therefore, in the range of--20, a "Deng" word is returned at the index position 14, there are two "Deng" characters in the range of 0 to 33, because lastindexof is the index location of the last matched item, so 32 is returned, instead of 14.

String. substring

String. substring Method
Retrieve the substring from this instance.

Name Description
String. substring (int32) Retrieve the substring from this instance. The substring starts from the specified character position.
String. substring (int32, int32) Retrieve the substring from this instance. The substring starts from the specified character position and has the 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); // return to "Deng shiwen of international communication * Deng shiwen of Shenzhen yingji Industrial Co., Ltd. of international communication"
Label1.text = Str. substring (); // The system returns the message "Deng shenwen of international communication *"

Summary:

Indexof and lastindexof both return an integer and an integer. If no value is found,-1 is returned;
Indexof is from left to right, lastindexof is from right to left, whether it is indexof or lastindexof, the index sequence is from left to right (the starting value is 0)
Substring is the string truncation, And the return value is the string after the truncation.

Related Article

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.