java中startswith的用法是什么
在Java中,startsWith()方法用于检查字符串是否以指定的前缀开头。该方法的用法如下:
public boolean startsWith(String prefix)
参数:
prefix:要检查的前缀字符串。
返回值:
如果字符串以指定的前缀开头,则返回true;
否则,返回false。
示例:
String str = "Hello, World!";
System.out.println(str.startsWith("Hello")); // true
System.out.println(str.startsWith("He")); // true
System.out.println(str.startsWith("ello")); // false
在上面的示例中,字符串"Hello, World!"以"Hello"和"He"开头,但不以"ello"开头。因此,startsWith()方法分别返回true、true和false。
阅读剩余
THE END