lastIndexOf()
int lastIndexOf(int ch)
- Returns the index within this string of the last occurrence of the specified character.
int lastIndexOf(int ch, int fromIndex)
- Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
int lastIndexOf(String str)
- Returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)
- Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
The following example shows how to use the various index methods to search inside of Strings:
public class Main {
public static void main(String args[]) {
String s = "this is a test.";
System.out.println(s);
System.out.println("indexOf(t) = " + s.indexOf('t'));
System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
System.out.println("indexOf(the) = " + s.indexOf("the"));
System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the"));
System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60));
System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
System.out.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the", 60));
}
}
Here is the output of this program:
this is a test.
indexOf(t) = 0
lastIndexOf(t) = 13
indexOf(the) = -1
lastIndexOf(the) = -1
indexOf(t, 10) = 10
lastIndexOf(t, 60) = 13
indexOf(the, 10) = -1
lastIndexOf(the, 60) = -1
Home
Java Book
Essential Classes
Java Book
Essential Classes
String:
- String type and Literals
- String Concatenation
- String.CASE_INSENSITIVE_ORDER
- String Constructor
- charAt(int index):Get a single char by index
- String: compareTo(String stringValue)
- concat(String str)
- equals():Compare two string value for equality
- equals( ) vs ==
- contains(CharSequence s)
- copyValueOf(char[] data)
- endsWith(String suffix)
- format():Format a string
- getBytes():Get byte array from string
- getChars()
- indexOf
- intern a string
- isEmpty:if string is empty
- lastIndexOf()
- length() Returns the length of this string
- startsWith( )
- toLowerCase() and toUpperCase(): convert string case with locale
- substring:Get sub string from a string
- toCharArray():Get char array from string
- toString( )
- trim()
- valueOf():Convert boolean, char, double, float,int,long,object to String