Advertisement
Methods | Description |
---|---|
public int indexOf(char ch) | It returns the first index of the char ch found in the String object. |
public int indexOf(String str)) | It returns the first 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.indexOf('v');
int index2 = str1.indexOf('a');
int index3 = str1.indexOf('z');
System.out.println("First Index of 'v' in " + str1 + " : "+ index1);
System.out.println("First Index of 'a' in " + str1 + " : "+ index2);
System.out.println("First Index of 'z' in " + str1 + " : "+ index3);
}
}
Index of 'v' in Java Rules, Java : 2
Index of 'a' in Java Rules, Java : 1
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.indexOf("Blue");
int index2 = str1.indexOf("Sky");
int index3 = str1.indexOf("Hi");
System.out.println("First Index of Blue in " + str1 + " : "+ index1);
System.out.println("First Index of Sky in " + str1 + " : "+ index2);
System.out.println("First Index of Hi in " + str1 + " : "+ index3);
}
}
First Index of Blue in Blue Sky Blue : 0
First Index of Sky in Blue Sky Blue : 5
First 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("Jazz joy");
String str2= new String("Live and let live");
int index1 = str1.indexOf('j');
int index2 = str2.indexOf("live");
System.out.println("First Index of j in " + str1 + " : "+ index1);
System.out.println("First Index of live in " + str2 + " : "+ index2);
}
}
Last Index of j in Jazz joy : 5
Last Index of live in live and let live : 13
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement