Advertisement
Methods | Description |
---|---|
public int lastIndexOf(char ch) | It returns the last index of the char ch found in the String object. |
public int lastIndexOf(String str)) | It returns the last index of the String str found in the String object. |
// Program to find a particular character in a String.
class StringIndex1
{
public static void main(String[] ar)
{
String str1= new String("Java Rules, Java");
int index1 = str1.lastIndexOf('v');
int index2 = str1.lastIndexOf('a');
int index3 = str1.lastIndexOf('z');
System.out.println("Last Index of 'v' in " + str1 + " : "+ index1);
System.out.println("Last Index of 'a' in " + str1 + " : "+ index2);
System.out.println("Last Index of 'z' in " + str1 + " : "+ index3);
}
}
Last Index of 'v' in Java Rules, Java : 14
Last Index of 'a' in Java Rules, Java : 15
Last Index of 'z' in Java Rules, Java : -1
// Program to find a particular character in a String.
class StringIndex1
{
public static void main(String[] ar)
{
String str1= new String("Blue Sky Blue");
int index1 = str1.lastIndexOf("Blue");
int index2 = str1.lastIndexOf("Sky");
int index3 = str1.lastIndexOf("Hi");
System.out.println("Last Index of Blue in " + str1 + " : "+ index1);
System.out.println("Last Index of Sky in " + str1 + " : "+ index2);
System.out.println("Last Index of Hi in " + str1 + " : "+ index3);
}
}
Last Index of Blue in Blue Sky Blue : 9
Last Index of Sky in Blue Sky Blue : 5
Last Index of Hi in Blue Sky Blue : -1
Advertisement
// Program to find a particular character in a String.
class StringIndex1
{
public static void main(String[] ar)
{
String str1= new String("Toys Story");
String str2= new String("Live and let live");
int index1 = str1.lastIndexOf('s');
int index2 = str2.lastIndexOf("live");
System.out.println("Last Index of s in " + str1 + " : "+ index1);
System.out.println("Last Index of live in " + str2 + " : "+ index2);
}
}
Last Index of s in Toys Story : 3
Last Index of live in Live and let live : 13
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement