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

如何在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("Welcome")) { 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中检查字符串是否以特定子字符串开头?的详细内容,更多请关注编程之家其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐