java中如何判断字符串是以什么开头

内容摘要
函数介绍:startsWith() 方法用于检测字符串是否以指定的前缀开始。语法:public boolean startsWith(String prefix, int toffset)或public boolean startsWith(String prefix)
文章正文

函数介绍:

startsWith() 方法用于检测字符串是否以指定的前缀开始。

语法:

public boolean startsWith(String prefix, int toffset)

public boolean startsWith(String prefix)

返回值:

如果字符串以指定的前缀开始,则返回 true;否则返回 false。

推荐免费学习视频:java免费视频教程

endsWith() 方法用于测试字符串是否以指定的后缀结束。

语法:

public boolean endsWith(String suffix)

返回值:

如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。

注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。

示例如下:

Java判断String是否以某个字符串开头:

String mobile = "8618730600000";
System.out.println(mobile.startsWith("86"));//输出true
System.out.println(mobile.startsWith("886"));//输出false

Java判断String是否以某个字符串结尾:

String mobile = "8618730600000";
System.out.println(mobile.endsWith("0000"));//输出true
System.out.println(mobile.endsWith("8888"));//输出false

想学习更多相关知识请访问:java入门教程

代码注释
[!--zhushi--]

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!