April 29, 2022

Search String in Another String in Java - indexOf, lastIndexOf, contains methods

If you want to check if given string is present in another string then in Java you have following options-

  1. Use indexOf() method to find the first occurrence of the specified character or substring. See example.
  2. Use lastIndexOf() method to get index within this string of the last occurrence of the specified character or substring. See example.
  3. Using contains() method in Java you can check if this string contains the specified substring or not. Returns true if substring is found false otherwise. See example.

Note that all of these methods do a case sensitive search so you may need to convert String and substring to similar case (lower or upper case) if you don’t want case to be a factor while searching.

Searching String using indexOf() method in Java

indexOf() method of the Java String class has 4 variants, two are used to search specified character and two are used to search specified substring.

  • int indexOf(int ch)- If found returns the index within this string of the first occurrence of the specified character, otherwise returns -1.
  • int indexOf(int ch, int fromIndex)- If found returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. Returns -1 if character is not found.
  • int indexOf(String str)- If found returns the index within this string of the first occurrence of the specified substring, otherwise returns -1.
  • int indexOf(String str, int fromIndex)- If found returns the index within this string of the first occurrence of the specified substring, starting at the specified index. Returns -1 if substring is not found.
Searching for character in a string using indexOf() example
public class StringSearch {
  public static void main(String[] args) {
    String str = "This is a test String";
    // Search for first occurrence
    int index = str.indexOf('s');
    System.out.println("First occurrence of character 's' found at index " + index);
    // Search for first occurrence after specified index
    index = str.indexOf('s', 11);
    System.out.println("First occurrence of character 's' after index 11 found at index " + index);
  }
}
Output
First occurrence of character 's' found at index 3
First occurrence of character 's' after index 11 found at index 12
Searching for substring in a string using indexOf() Java example
public class StringSearch {
  public static void main(String[] args) {
    String str = "This is a test String";
    // Search for first occurrence
    int index = str.indexOf("test");
    if(index != -1) {
      System.out.println("First occurrence of substring test found at index " + index);
    }else {
      System.out.println("Substring not found ");
    }
    
    // Search for first occurrence after specified index
    index = str.indexOf("test", 6);
    System.out.println("First occurrence of substring test after index 6 found at index " + index);
  }
}
Output
First occurrence of substring test found at index 10
First occurrence of substring test after index 6 found at index 10

Searching String using lastIndexOf() method in Java

lastIndexOf() method of the Java String class has 4 variants, two are used to search specified character and two are used to search specified substring.
  • int lastIndexOf(int ch)- If found returns the index within this string of the last occurrence of the specified character, otherwise returns -1.
  • int lastIndexOf(int ch, int fromIndex)- If found returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. Returns -1 if character is not found.
  • int lastIndexOf(String str)- If found returns the index within this string of the last occurrence of the specified substring, otherwise returns -1.
  • int lastIndexOf(String str, int fromIndex)- If found returns the index within this string of the last occurrence of the specified substring, searching backward at the specified index. Returns -1 if substring is not found.
Searching for character in a string using lastIndexOf() example
public class StringSearch {
  public static void main(String[] args) {
    String str = "This is a test String";
    // Search for last occurrence
    int index = str.lastIndexOf('s');
    System.out.println("Last occurrence of character 's' found at index " + index);
    // Search for last occurrence after specified index
    index = str.lastIndexOf('s', 11);
    System.out.println("Last occurrence of character 's' moving backward from index 11 found at index " + index);
  }
}
Output
Last occurrence of character 's' found at index 12
Last occurrence of character 's' moving backward from index 11 found at index 6
Searching for substring in a string using lastIndexOf() example
public class StringSearch {
  public static void main(String[] args) {
    String str = "test String to test";
    // Search for last occurrence
    int index = str.lastIndexOf("test");
    if(index != -1) {
      System.out.println("Last occurrence of substring test found at index " + index);
    }else {
      System.out.println("Substring not found ");
    }		
    // Search for last occurrence after specified index
    index = str.lastIndexOf("test", 6);
    System.out.println("Last occurrence of substring test moving backward from index 6 found at index " + index);
  }
}
Output
Last occurrence of substring test found at index 15
Last occurrence of substring test moving backward from index 6 found at index 0

Searching String using contains() method in Java

  • boolean contains(CharSequence s)- Returns true if and only if this string contains the specified sequence of char values, false otherwise.
CharSequence is an interface which is implemented by String, StringBuffer and StringBuilder so objects of these classes can be passed with contains() method.
public class StringSearch {
  public static void main(String[] args) {
    String str = "This is a test String";
    String str1= "test";
    if(str.contains(str1)) {
      System.out.println(str1 + " found in String");
    }else {
      System.out.println(str1 + "is not found in String");
    }
  }
}
Output
test found in String
If you do a search for “Test” false is returned as the search is case sensitive.
public class StringSearch {
  public static void main(String[] args) {
    String str = "This is a test String";
    String str1= "Test";
    if(str.contains(str1)) {
      System.out.println(str1 + " found in String");
    }else {
      System.out.println(str1 + " is not found in String");
    }
  }
}
Output
Test is not found in String

That's all for the topic Search String in Another String in Java - indexOf, lastIndexOf, contains methods. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment