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.