在Java中,startsWith()方法用于检查字符串是否以指定的前缀开头
在Java中,startsWith()方法用于检查字符串是否以指定的前缀开头。该方法的用法如下:
publicbooleanstartsWith(Stringprefix)
参数:
- prefix:要检查的前缀字符串。
返回值:
- 如果字符串以指定的前缀开头,则返回true;
- 否则,返回false。
示例:
Stringstr="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。
版权声明
本文仅代表作者观点,不代表博信信息网立场。
上一篇:css图片怎么右对齐显示 下一篇:用原生JS怎样实现分页效果,有哪些要点