Java indexOf () lastIndexOf () string lookup function

Source: Internet
Author: User

Use indexOf () to match a string

Returns the first occurrence of a substring in a String object.

StrObj. indexOf (subString [, startIndex])
Parameters
StrObj
Required. String object or text.
SubString
Required. The substring to be searched in the String object.
StarIndex
Optional. This integer indicates the index in the String object. If it is omitted, it is searched from the beginning of the string.
Description
The indexOf method returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned.

If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.

 

Public class MainClass {

Public static void main (String [] arg ){
String str = "abcde ";
   
Int index = 0;
Index = str. indexOf ('C ');

System. out. println (index );
  }

}


Instance 2

Public class MainClass {

Public static void main (String [] arg ){
String str = "abcdea ";
   
Int index = 0;
Index = str. lastIndexOf ('A ');

System. out. println (index );
  }

}

Searches for the position of the specified character string.

Public class MainClass {

Public static void main (String [] arg ){
String str = "abcdea ";
Int startIndex = 3;
   
Int index = 0;
Index = str. indexOf ('A', startIndex );

System. out. println (index );
  }

}

End of the string

Public int lastIndexOf (String str, int fromIndex)
// Start searching backward from the specified index and return the index of the last occurrence of the specified sub-string in this string.
// K <= Math. min (fromIndex, str. length () & this. startsWith (str, k)
// Find the last position of str in String! If the location is <= fromIndex, return-1. Otherwise, return-1.
Your scenario is:
// The last occurrence of One in banner is at 9
// And 9 <10, so 9 is returned.

Public class MainClass {

Public static void main (String [] arg ){
String str = "abcdeabcdef ";
  
Int index = 0;
Index = str. lastIndexOf ("AB ");

System. out. println (index );
  }

}

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.