Java String indexOf() Method

The indexof() methods return the index of the first occurrence of the specified character or return -1 if the character does not occur. The syntax for the indexOf() method in String class is as follows:- int indexOf(char ch)

There are also many methods that can be used for our convenience. Let us see an example of indexof() method.

String string = "I Love programming";
System.out.println(string.indexOf('a'));

The above code returns 12 because the character ‘a’ exists at the 12th index in the given string. Here count or index starts from 0.

1) public String indexOf(int ch)

  • Parameter: character
  • Return value: an integer value that is the index of the specified string
  • Throws: IndexOutOfBound Exception if the given index is more than the string value.

2) public string lastIndexOf(int ch)

  • Parameter: A character
  • Return value: an integer value of the last occurrence of the specified character.
  • Throws: IndexOutOfBound Exception if the given index is more than the string value.

The lastIndexOf In Java

The lastIndexOf() method returns the last occurrence of the specified character, java provides many other ways to find the last index of the character.

i) Using lastIndexOf(int ch)

public class Main {
   public static void main(String args[]) {
      String string = "I Love programming";
      System.out.println(string.lastIndexOf('g'));
   }
}

Output:-

17

ii) Using lastIndexOf(int  ch, int fromIndex)

This method takes an extra argument integer formIndex that is it returns the character from the specified index. Usually, lastIndexOf(char ch) returns the last occurrence of the character but this lastIndexOf(char ch, int fromIndex) returns the character from the index specified.

public class Main {
   public static void main(String args[]) {
      String s = "I Love programming";
      System.out.println(s.lastIndexOf('g', 10));
   }
}

Output:-

10

iii) Using lastIndexOf(String str)

This method accepts the whole string as an argument and returns the index of the first character of the last substring. 

public class Main {
   public static void main(String args[]) {
      String s = "I Love programming";
      System.out.println(s.lastIndexOf("Love"));
   }
}

Output:

2

iv) Using lastIndexOf(String str, int fromIndex)

This method searches for a string in the specified index if not present returns -1. The program searches for the string ”Java” which is not present in the given string hence it returns -1.

public class Main {
   public static void main(String args[]) {
      String s = "I Love programming";
      System.out.println(s.lastIndexOf("Java", 3));
   }
}

Output:

-1

Find Character In String Java

To find a character in a string we usually use the indexOf() method which returns the first occurrence of the character. There are various ways to find this.

i) Using indexOf(int ch)

It returns the index position of the given character, usually, it returns the character in the first occurrence. In the below example there are many ‘a’ in the string but it returns the ‘a’ in the first position.

public class Main {
   public static void main(String args[]) {
      String string = "Java is a good pogramming language";
      System.out.println(string.indexOf("a"));
   }
}

Output:

1

ii) Using indexOf(int ch, int fromIndex)

For this method indexOf(int ch, int formIndex) we need to pass two parameters, the first one is character and the second one is index form which the method should return.

Write A Java Program To Find the First Occurrence Of A Character In A Given String

public class Main {
   public static void main(String args[]) {
      String string = "Java is a good rpogramming language";
      System.out.println(string.indexOf("a", 7));
   }
}

Output:

8

iii) Using indexOf(string str)

This method takes a string as a parameter and returns the position of the given substring.

public class Main {
   public static void main(String args[]) {
      String string = "Java is a good pogramming language";
      System.out.println(string.indexOf("good"));
   }
}

Output:

10

iv) Using indexOf(String str, int fromIndex)

This method takes two parameter strings and a starting index and returns the string from that index. In the below program there are two “is” string but the code returns the “is” in the 26th position as mentioned.

public class Main {
   public static void main(String args[]) {
      String s = "Java is a good pogramming is language";
      System.out.println(s.indexOf("is", 25));
   }
}

Output:

26

Get Char From String Java

To get a character from a string we use the charAt() method, this method takes one parameter that is an integer and returns the character in that index.

public class Main {
   public static void main(String args[]) {
      String string = "Java is a good pogramming is language";
      System.out.println(string.charAt(0));
   }
}

Output:

J

How To Get Particular Word From String In Java

Here, we extract words from the string in Java by using the split() method.

public class Main {
   public static void main(String args[]) {
      String string = "Java Programming";
      String[] words = string.split(" ", 3);
      for (String word : words) {
         System.out.println(word);
      }
   }
}

Output:

Java
Programming

Getting A Substring From A String After A Particular Word In Java

In this section we extract a  substring after a given substring, using a user-defined function that takes two string parameters value and a string.

public class Main {
   public static String after(String value, String a) {
      int posA = value.lastIndexOf(a);
      if (posA == -1) {
         return "";
      }
      int adjustedPosA = posA + a.length();
      if (adjustedPosA >= value.length()) {
         return "";
      }
      return value.substring(adjustedPosA);
   }

   public static void main(String args[]) {
      String s = "I Love Programming";
      System.out.println(after(s, "I "));
      System.out.println(after(s, "I Love "));
   }
}

Output:-

Love Programming
Programming

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.