Java的String类中的startsWith方法和endsWith方法示例详解
更新时间:2023年11月01日 10:23:25 作者:wangqiuyue---
大家应该都知道startsWith()方法用于检测字符串是否以指定的前缀开始,endsWith()方法用于测试字符串是否以指定的后缀结束,本文就Java的String类中的startsWith方法和endsWith方法给大家详细讲解,感兴趣的朋友一起看看吧
1、startsWith()方法
用于检测字符串是否以指定的前缀开始。
语法:
public boolean startsWith(String prefix, int toffset) public boolean startsWith(String prefix) 参数 prefix -- 前缀。 toffset -- 字符串中开始查找的位置。 返回值 如果字符串以指定的前缀开始,则返回 true;否则返回 false。
举例:
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.startsWith("www") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("runoob") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("runoob", 4) );
}
}以上程序执行结果为:
返回值 :true
返回值 :false
返回值 :true
2.endsWith()方法
endsWith()方法用于测试字符串是否以指定的后缀结束。
语法如下:
public boolean endsWith(String suffix) 参数 suffix -- 指定的后缀。 返回值 如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。
举例:
public class Test {
public static void main(String args[]) {
String Str = new String("菜鸟教程:www.runoob.com");
boolean retVal;
retVal = Str.endsWith( "runoob" );
System.out.println("返回值 = " + retVal );
retVal = Str.endsWith( "com" );
System.out.println("返回值 = " + retVal );
}
}结果如下:
返回值 = false
返回值 = true
到此这篇关于Java的String类中的startsWith方法和endsWith方法的文章就介绍到这了,更多相关java startsWith方法和endsWith方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)
本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数据库密码)进行加密,通过引入Jasypt依赖、配置加密密钥、加密敏感信息并测试解密功能,可以提高配置文件的安全性,减少因配置文件泄露导致的安全风险,感兴趣的朋友一起看看吧2025-03-03
idea2020.1版本git提交项目到github上的方法
这篇文章主要介绍了idea2020.1版本git提交项目到github上的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2020-06-06
解决mybatis generator MySQL自增ID出现重复问题MySQLIntegrityC
在MySQL中使用MyBatis时,可能会遇到由于主键重复导致的插入失败问题,此问题通常发生在连续插入多条数据时,如果selectKey的order配置错误,如使用BEFORE而不是AFTER,将会导致获取的ID未更新,引起主键重复错误,正确的配置应使用AFTER2024-10-10


最新评论