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"));//输出truesystem.out.println(mobile.startswith("886"));//输出false
java判断string是否以某个字符串结尾:
string mobile = "8618730600000";system.out.println(mobile.endswith("0000"));//输出truesystem.out.println(mobile.endswith("8888"));//输出false
想学习更多相关知识请访问:java入门教程
以上就是java中如何判断字符串是以什么开头的详细内容。
