Java String Search

In this chapter you will learn:

  1. How to check if a string contains in another string
  2. How to check if this string ends with the specified suffix
  3. How to test if a string starts with another string
  4. How to search string from start
  5. How to search a string from the end

Check the existence of a substring

boolean contains(CharSequence s) returns true if and only if this string contains the specified sequence of char values.

public class Main {
  public static void main(String[] argv) {
    String str = "Demo2s.com";
    String str2 = "demo2s.com";
    System.out.println(str.contains(str2));
/* ja  v  a 2  s. com*/
  }
}

The output:

If this string ends with the specified suffix

boolean endsWith(String suffix) tests if this string ends with the specified suffix.

public class Main {
  public static void main(String[] argv) {
    String str = "Demo2s.com";
    String str2 = ".com";
    System.out.println(str.endsWith(str2));
//from   j a v a 2 s  .  c  om
  }
}

The output:

If this string starts with the specified prefix

The following two methods test if a string starts with another string. You can even specify an offset for the searching.

  • boolean startsWith(String prefix) tests if this string starts with the specified prefix.
  • boolean startsWith(String prefix, int startIndex) tests if the substring of this string beginning at the specified index starts with the specified prefix.

startIndex is the index to start with. For example,

public class Main{
   public static void main(String[] argv){
      System.out.println("Foobar".startsWith("bar", 3));
    }
}

Search string from start

We can search a string from start in another string.

  • int indexOf(int ch) returns the index within this string of the first occurrence of the specified character.
  • int indexOf(int ch, int fromIndex) returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
  • int indexOf(String str) returns the index within this string of the first occurrence of the specified substring.
  • int indexOf(String str, int fromIndex) returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
public class Main {
  public static void main(String[] argv) {
    String str = "Java2s.com";
    String str2 = ".com";
    System.out.println(str.indexOf(str2));
/*from j a  va 2  s  . c  om*/
  }
}

The output:

Search a string from the end

We can search a string from the end by using the following methods:

  • 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.";
//j av a 2  s. c  o m
    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:

Next chapter...

What you will learn in the next chapter:

  1. How to Get a single char by index
  2. How to copy part of a char array to String
  3. How to Get char array from string
  4. How to get char or char array from a string
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words