Java String Search
In this chapter you will learn:
- How to check if a string contains in another string
- How to check if this string ends with the specified suffix
- How to test if a string starts with another string
- How to search string from start
- 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:
- How to Get a single char by index
- How to copy part of a char array to String
- How to Get char array from string
- How to get char or char array from a string