String lookup function for java learning Notes

Source: Internet
Author: User

In java, check whether a String contains one character or a String of java. lang. String

IndexOf method

The code is as follows: Copy code

Package com. java. lang;
 
Public class StringX {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
Java. lang. String a = "my name is yuexiaosheng, my blog is java-er.com ";
Int isPos = a. indexOf ("is ");
Int islastPos = a. lastIndexOf ("is ");
 
System. out. println ("first 'is 'postion is" + isPos );
System. out. println ("last 'is 'postion is" + islastPos );
 
Int haswen = a. indexOf ("? ");
System. out. println (haswen );
If (haswen =-1 ){
System. out. println ("no question mark in the string ");
     }
 
 }
 
}
Int indexOf (int ch)

Returns the index of the first occurrence of the specified character in this string.

Int indexOf (int ch, int fromIndex)

Searches from the specified index and returns the index at the specified character for the first time in this string.

Int indexOf (String str)

Returns the index of the first occurrence of the specified substring in this string.

Int indexOf (String str, int fromIndex)

Returns the index of the first occurrence of the specified substring from the specified index.

String intern ()

Returns the canonicalized representation of string objects.

Int lastIndexOf (int ch)

Returns the index of the last occurrence of the specified character in this string.

Int lastIndexOf (int ch, int fromIndex)

Start backward search from the specified index, and return the index of the last occurrence of the specified character in this string.

Int lastIndexOf (String str)

Returns the index of the rightmost substring in this string.

Int lastIndexOf (String str, int fromIndex)

Searches backward from the specified index and returns the index of the specified sub-string that appears the last time in this string.

Int length ()

Returns the length of this string.

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.