Linux 拨号vps windows公众号手机端

java中startswith的用法是什么

lewis 8年前 (2017-02-07) 阅读数 11 #程序编程
文章标签 Java

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门