Java中检查字符串是否以特定字符结尾
作者:? 时间:2021-08-07 20:04:47
前言:
在日常开发中经常会遇到字符串匹配问题,我们就来学习使用Java中的一些方便快捷的方法来解决这个问题吧
使用String类
Java自带的字符串类提供了多种方法来验证一个给定的字符串是否以特定的字符串结束
endsWith()方法
endsWith()
方法就是为了解决这个问题而引入的。它提供了一个直接的方法来检查一个String对象是否以另一个字符串结尾
public static boolean isEndWith(String text, String suffix) {
if (text == null || suffix == null) {
return false;
}
return text.endsWith(suffix);
}
注意:需要确保text和suffix不为空,以避免出现空指针异常NullPointerException
matches()方法
matches()
方法也可以用来解决这个问题,它使用正则检查一个字符串是否与一个给定的正则表达式相匹配
public static boolean isEndWith(String text, String suffix) {
if (text == null || suffix == null) {
return false;
}
String regex = ".*" + suffix + "$";
return text.matches(regex);
}
这里使用了一个正则表达式来匹配我们字符串文本末尾的后缀($)。然后我们把这个正则表达式传给了matches()
方法执行进行匹配
regionMatches()
还可以使用regionMatches()
方法来解决这个问题,regionMatches()
方法用于检测两个字符串在一个区域内是否相等。
public static boolean usingStringRegionMatchesMethod(String text, String suffix) {
if (text == null || suffix == null) {
return false;
}
int toffset = text.length() - suffix.length();
return text.regionMatches(toffset, suffix, 0, suffix.length());
}
toffset表示text字符串中子区域的起始偏移量。所以为了检查text是否以suffix结束,toffset应该等于text的长度减去suffix的长度
使用Pattern类
我们还可以使用Pattern类来编译一个正则表达式,来检查text是否以suffix结束
重新使用上边使用过的正则表达式:
public static boolean usingPatternClass(String text, String suffix) {
if (text == null || suffix == null) {
return false;
}
Pattern pattern = Pattern.compile(".*" + suffix + "$");
return pattern.matcher(text).find();
}
Pattern编译了前面的正则,表示以字符串suffix结尾,并将其与我们的text相匹配
来源:https://juejin.cn/post/7105404224170623013
标签:Java,特定,字符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java+swing实现抖音上的表白程序详解
2023-11-15 21:34:08
![](https://img.aspxhome.com/file/2023/6/76196_0s.png)
ToLua框架下C#与Lua代码的互调操作
2023-12-07 06:27:48
![](https://img.aspxhome.com/file/2023/7/67567_0s.jpg)
深入Unix时间戳与C# DateTime时间类型互换的详解
2023-06-04 21:48:11
JPA 加锁机制及@Version版本控制方式
2022-10-06 10:57:58
java异常处理的简单练习
2023-12-01 02:20:48
java简单实现斗地主发牌功能
2023-06-18 16:22:44
![](https://img.aspxhome.com/file/2023/6/57506_0s.jpg)
Java实现FTP批量大文件上传下载篇2
2021-07-22 02:30:31
![](https://img.aspxhome.com/file/2023/3/83123_0s.jpg)
解决MyEclipse10.7部署报错抛空指针异常问题的方法
2023-10-14 23:52:34
![](https://img.aspxhome.com/file/2023/5/82315_0s.jpg)
深入解析Java的设计模式编程中的模板方法模式
2023-11-01 13:31:14
![](https://img.aspxhome.com/file/2023/5/87205_0s.jpg)
C++实现LeetCode(5.最长回文子串)
2023-07-03 06:07:13
Java class文件格式之常量池_动力节点Java学院整理
2023-04-14 07:14:06
![](https://img.aspxhome.com/file/2023/2/66472_0s.jpg)
springboot-mybatis/JPA流式查询的多种实现方式
2021-07-07 17:25:51
![](https://img.aspxhome.com/file/2023/2/76292_0s.jpg)
Android自定义View之组合控件实现类似电商app顶部栏
2023-07-26 22:14:10
![](https://img.aspxhome.com/file/2023/3/84713_0s.png)
Spring实战之协调作用域不同步的Bean操作示例
2023-01-23 02:48:10
基于C#实现手机号码归属地接口调用
2022-07-14 09:46:54
深入学习Java单元测试(Junit+Mock+代码覆盖率)
2021-06-17 00:39:13
100行C#代码实现经典扫雷游戏
2023-12-05 16:33:30
![](https://img.aspxhome.com/file/2023/0/70400_0s.png)
关于Maven的使用,这些你都真的了解么
2022-01-02 14:19:08
Android提高之BroadcastReceiver实例详解
2021-12-13 20:22:22
![](https://img.aspxhome.com/file/2023/9/85979_0s.jpg)
采用C#实现软件自动更新的方法
2021-12-30 19:13:38