首页  >  文章  >  Java  >  如何在Java中检查字符串是否以特定子字符串开头?

如何在Java中检查字符串是否以特定子字符串开头?

PHPz
PHPz转载
2023-09-09 19:13:041104浏览

如何在Java中检查字符串是否以特定子字符串开头?

String 类可以用来表示字符串,Java程序中的所有字符串文字都是作为一个实例实现的一个字符串类。字符串是常量,它们的值一旦创建就无法更改不可变)。

我们可以使用 startsWith( String 类的 ) 方法检查字符串是否以特定字符串开头,它返回一个布尔值, true 或 false。

语法
public boolean startsWith(String prefix)

示例

public class StringStartsWithSubStringTest {
   public static void main(String[] args) {
      String str = "WelcomeToTutorialsPoint";
      if(str.startsWith("<strong>Welcome</strong>")) {
         System.out.println("Begins with the specified word!");
      }
      else {
         System.out.println("Does not begin with the specified word!");
      }
   }
}

输出

Begins with the specified word!

以上是如何在Java中检查字符串是否以特定子字符串开头?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除