Java startsWith() Method
The startsWith() method is used to check whether a string starts with the specified prefix.
Syntax
public boolean startsWith(String prefix, int toffset) 或 public boolean startsWith(String prefix)
Parameters
prefix-- Prefix.
toffset-- The position in the string to start the search.
Return Value
Returns true if the string starts with the specified prefix; otherwise returns false.
Example
public class Test {
public static void main(String args[]) {
String Str = new String("www.example.com");
System.out.print("Return Value:" );
System.out.println(Str.startsWith("www") );
System.out.print("Return Value:" );
System.out.println(Str.startsWith("example") );
System.out.print("Return Value:" );
System.out.println(Str.startsWith("example", 4) );
}
}
The above program produces the following result:
返回值 :true 返回值 :false 返回值 :true
Other Extensions
Java String Class