Find Strings in Java

Source: Internet
Author: User

Find Strings in Java

The string class provides two methods for finding strings that allow you to search for a specified character or string in a string, where the IndexOf () method is used to search for the first occurrence of a character or substring, and the LastIndexOf () method is used to search for character or substring lookup strings
The string class provides two methods for finding strings that allow you to search for a specified character or string in a string, where the IndexOf () method is used to search for the first occurrence of a character or substring, and the LastIndexOf () method is used to search for the last occurrence of a character or substring.
These two methods are overloaded by several different methods. In all these cases, the method returns the subscript of the position where the character or substring was found, and returns −1 when the search fails.
indexOf (int ch) method
This method is used to get the index at the first occurrence of the specified character in the string, and return −1 if the character does not appear.
Str.indexof (CH)
Ch: The character to search for.
STR: A String object.
LastIndexOf (int ch) method
The method user gets the index at the last occurrence of the specified character in the string, and returns −1 if the character does not appear.
Str.lastindexof (CH)
Ch: The character to search for.
STR: A String object.
IndexOf (String str) method
This method is used to get the index position of the first occurrence of the substring specified in the string, and if the substring appears as a substring of the string, returns the index of the first character of the substring, and returns −1 if it does not appear as a substring.
Str.indexof (SUBSTR)
SUBSTR: The string to search for.
STR: A String object.
LastIndexOf (String str) method
This method is used to get the index position of the last occurrence of the substring specified in the string. If the substring appears one or more times as a substring of a string, it returns the index of the first character of the last occurrence of the substring. Returns −1 if it is not present as a substring.
Str.lastindexof (SUBSTR)
STR: A String object.
SUBSTR: The substring of the search.
Attention:
Here the substring is specified by substr. Returns the index where the substring appears at the very right end of the string. The last empty string "" will be considered to appear in the return position of the This.length () method (that is, the length of the string).
None of the above 4 types of methods can specify the starting point for the search, and several other overloaded methods are described below to search for content based on the starting point of the specified search.
indexOf (int ch,int startIndex)
This method searches for the location of the first occurrence of the specified character, based on the specified search starting point.
Grammar:
IndexOf (Ch,startindex)
Ch: The character to search for.
StartIndex: The index at which to start the lookup location.
LastIndexOf (CH, startIndex) method
This method searches for the last occurrence of the specified character, based on the specified search starting point.
Grammar:
int LastIndexOf (CH,STARTINDEX)
Ch: The character to search for.
StartIndex: The index at which to start the lookup location.
IndexOf (String str, int startIndex) method
This method searches for the first occurrence of the specified string, based on the specified search starting point.
IndexOf (Str,startindex)
STR: The substring to search for.
StartIndex: The index at which to start the lookup location.
LastIndexOf (String str, int startIndex) method
This method searches for the last occurrence of the substring, based on the specified search start point.
LastIndexOf (Str,startindex)
STR: The substring to search for.
StartIndex: The index at which to start the lookup location.
The following examples illustrate various search methods for strings. For example, the starting and ending positions of characters or strings specified in the search string. The implementation steps for the instance are as follows.
(1) Create a Strsearch class in which the string variable, str, and integer variable rs are defined respectively. Then output string Str's various search methods return results to the console.
Src\com\lzw\strsearch.java key code is as follows:
public static void Main (string[] args) {
String str= "Mingrikeji"; Creating a String
int Rs=str.indexof (' i ');
System.out.println ("The character I first appears in the position is:" +rs);
Rs=str.lastindexof (' i ');
The System.out.println ("character I last appearance" position is: "+rs");
Rs=str.lastindexof ("Ri");
System.out.println (the first occurrence of the string ri is: "+rs);"
Rs=str.lastindexof ("Ri");
System.out.println (the last occurrence of the string I is: "+rs);"
Rs=str.lastindexof (' I ', 4);
System.out.println ("from the 5 characters, the character I first appears in the position is:" +rs);
}
Src\com\lzw\strsearch.java Complete code:
Package COM.LZW;
public class Strsearch {
public static void Main (string[] args) {
String str= "Mingrikeji";
int Rs=str.indexof (' i ');
System.out.println ("The character I first appears in the position is:" +rs);
Rs=str.lastindexof (' i ');
The System.out.println ("character I last appearance" position is: "+rs");
Rs=str.lastindexof ("Ri");

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.