学堂 学堂 学堂公众号手机端

在Java中,startsWith()方法用于检查字符串是否以指定的前缀开头

lewis 1年前 (2024-03-27) 阅读数 6 #技术

在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。

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门