Search a sub string inside a string

ReturnMethodMeaning
booleancontains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.
booleancontentEquals(CharSequence cs)Compares this string to the specified CharSequence.
booleancontentEquals(StringBuffer sb)Compares this string to the specified StringBuffer.
intindexOf(int ch)Returns the index within this string of the first occurrence of the specified character.
intindexOf(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.
intindexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
intindexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
intlastIndexOf(int ch)Returns the index within this string of the last occurrence of the specified character.
intlastIndexOf(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.
intlastIndexOf(String str)Returns the index within this string of the rightmost occurrence of the specified substring.
intlastIndexOf(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.
booleanstartsWith(String prefix)Tests if this string starts with the specified prefix.
booleanstartsWith(String prefix, int toffset)Tests if the substring of this string beginning at the specified index starts with the specified prefix.
booleanendsWith(String suffix)Tests if this string ends with the specified suffix.

public class Main {
  public static void main(String[] argv) {
    String str = "Java2s.com";
    String str2 = "java2s.com";
    System.out.println(str.contains(str2));

  }
}

The output:


false

public class Main {
  public static void main(String[] argv) {
    String str = "Java2s.com";
    String str2 = ".com";
    System.out.println(str.endsWith(str2));

  }
}

The output:


true

public class Main {
  public static void main(String[] argv) {
    String str = "Java2s.com";
    String str2 = ".com";
    System.out.println(str.indexOf(str2));

  }
}

The output:


6

A second form of startsWith( ) lets you specify a starting point:


boolean startsWith(String str, int startIndex)

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));
    }
}

true

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.";

    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:


this is a test.
indexOf(t) = 0
lastIndexOf(t) = 13
indexOf(the) = -1
lastIndexOf(the) = -1
indexOf(t, 10) = 10
lastIndexOf(t, 60) = 13
indexOf(the, 10) = -1
lastIndexOf(the, 60) = -1
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.