Return | Method | Meaning |
---|---|---|
boolean | contains(CharSequence s) | Returns true if and only if this string contains the specified sequence of char values. |
boolean | contentEquals(CharSequence cs) | Compares this string to the specified CharSequence. |
boolean | contentEquals(StringBuffer sb) | Compares this string to the specified StringBuffer. |
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. |
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. |
boolean | startsWith(String prefix) | Tests if this string starts with the specified prefix. |
boolean | startsWith(String prefix, int toffset) | Tests if the substring of this string beginning at the specified index starts with the specified prefix. |
boolean | endsWith(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. |