There are four ways to find string substrings in Java (indexof ())

Source: Internet
Author: User

The IndexOf method returns an integer value that indicates the starting position of the substring in the string object. If no substring is found, 1 is returned.
if the startindex is negative, then startindex is treated as zero. If it is larger than the maximum character position index, it is treated as the largest possible index.

JavaThere are four ways to find a substring in a string, as follows:
1,int indexOf (String str): Returns the index of the specified substring that appears for the first time in this string.  
2,int indexOf (String str, int startIndex): Returns the index in this string for the first occurrence of the specified substring, starting at the specified index.  
3,int lastIndexOf (String str): Returns the index of the specified substring that appears rightmost in this string.  
4,int lastIndexOf (String str, int startIndex): Searches backward at the specified index, returning the index of the specified substring that last occurred in this string.

 PublicclassTest { PublicStaticvoidMain (string[] args) {String s= "Xxccxxxxx"; //find out if the specified character is present from the beginning//The results are as followsSystem.out.println (S.indexof ("C"));//2//continues the search from the fourth character position, including the current positionSystem.out.println (S.indexof ("C", 3));//3//The system returns 1 if the character is not in the specified string.System.out.println (S.indexof ("Y"));//-1System.out.println (S.lastindexof ("X"));//6    }  }

PrivateStaticvoidTestindexof () {string string= "Aaa456ac"; //finds that the specified character is the subscript in the string.  The string to be returned is the subscript; 1 is returned. System.out.println (String.IndexOf ("B"));//indexOf (String str); return result:-1, "B" does not exist//continues the search from the fourth character position, including the current positionSystem.out.println (String.IndexOf ("A", 3));//indexOf (String str, int fromIndex); Results returned: 6//(The difference from the previous: the above parameter is of type String, the following argument is of type int) reference data: a-97,b-98,c-99//find out if the specified character is present from the beginningSystem.out.println (String.IndexOf (99));//indexOf (int ch); return Result: 7System.out.println (String.IndexOf (' C '));//indexOf (int ch); return Result: 7//Find ch from fromindex, this is a character variable, not a string.  The number that corresponds to character a is 97. System.out.println (String.IndexOf (97,3));//indexOf (int ch, int fromIndex); return Result: 6System.out.println (String.IndexOf (' A ', 3));//indexOf (int ch, int fromIndex); return Result: 6//This is the flexibility to use the method provided by the string class to split the supplied string. //String s = "d:\\android\\sdk\\add-ons"; //System.out.println (s); //While (S.lastindexof ("\ \") > 0) {//s = s.substring (0, s.lastindexof ("\ \")); //System.out.println (s); //}  }

There are four ways to find string substrings in Java (indexof ())

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.