Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾

Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾

In Java

String.endsWith

oolean     endsWith(String suffix)
          测试此字符串是否以指定的后缀结束。

 

StringUtils.endsWith & StringUtils.endsWithIgnoreCase & StringUtils.endsWithAny

org.apache.commons.lang.StringUtils endsWith方法 写道
public static boolean endsWith(String str, String suffix)

Check if a String ends with a specified suffix.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils.endsWith(null, null) = true
StringUtils.endsWith(null, "def") = false
StringUtils.endsWith("abcdef", null) = false
StringUtils.endsWith("abcdef", "def") = true
StringUtils.endsWith("ABCDEF", "def") = false
StringUtils.endsWith("ABCDEF", "cde") = false


Parameters:
str - the String to check, may be null
suffix - the suffix to find, may be null
Returns:
true if the String ends with the suffix, case sensitive, or both null

 

org.apache.commons.lang.StringUtils endsWithIgnoreCase方法 写道
public static boolean endsWithIgnoreCase(String str, String suffix)

Case insensitive check if a String ends with a specified suffix.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case insensitive.

StringUtils.endsWithIgnoreCase(null, null) = true
StringUtils.endsWithIgnoreCase(null, "def") = false
StringUtils.endsWithIgnoreCase("abcdef", null) = false
StringUtils.endsWithIgnoreCase("abcdef", "def") = true
StringUtils.endsWithIgnoreCase("ABCDEF", "def") = true
StringUtils.endsWithIgnoreCase("ABCDEF", "cde") = false


Parameters:
str - the String to check, may be null
suffix - the suffix to find, may be null
Returns:
true if the String ends with the suffix, case insensitive, or both null

 

org.apache.commons.lang.StringUtils endsWithAny方法 写道
public static boolean endsWithAny(String string, String[] searchStrings)

Check if a String ends with any of an array of specified strings.

StringUtils.endsWithAny(null, null) = false
StringUtils.endsWithAny(null, new String[] {"abc"}) = false
StringUtils.endsWithAny("abcxyz", null) = false
StringUtils.endsWithAny("abcxyz", new String[] {""}) = true
StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true
StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true


Parameters:
string - the String to check, may be null
searchStrings - the Strings to find, may be null or empty
Returns:
true if the String ends with any of the the prefixes, case insensitive, or both null
 

In Bash

使用[[ ]] 模式匹配来判断是否以别的字符串结尾(推荐方式)

格式:[[ $STR == *$SUFFIX ]]

 

[root@web ~]# STR=hello.gif
[root@web ~]# SUFFIX=.gif
[root@web ~]# [[ $STR == *$SUFFIX ]] && echo "ends"
ends
[root@web ~]# 

 

使用[[ ]] 正则表达式匹配来判断是否以别的字符串结尾

格式:[[ $STR =~ $SUFFIX$ ]]

在正则表达式中,$匹配结尾。

 

[root@web ~]# STR=hello.gif
[root@web ~]# SUFFIX=.gif
[root@web ~]# [[ $STR =~ $SUFFIX$ ]] && echo "ends"
ends
[root@web ~]# 

 

例外:在上面的例子中,SUFFIX中包含点(.),而点(.)在正则表达式中能够匹配任意字符,如下所示:

[root@web ~]# STR=helloxgif
[root@web ~]# SUFFIX=.gif
[root@web ~]# [[ $STR =~ $SUFFIX$ ]] && echo "ends"
ends
[root@web ~]#

 

用case语句来判断是否以别的字符串结尾

正确:case "$STR" in *"$SUFFIX") echo "ends"; esac

错误:case "$STR" in "*$SUFFIX") echo "ends"; esac

注意*不能写在双引号里面,否则不灵。

 

 

[root@web ~]# STR=hello.gif
[root@web ~]# SUFFIX=.gif
[root@web ~]# case "$STR" in *"$SUFFIX") echo "ends"; esac
ends
[root@web ~]# case "$STR" in "*$SUFFIX") echo "ends"; esac
[root@web ~]#

 

用去尾法判断是否以别的字符串结尾

格式:[ "${STR%$SUFFIX}" != "$STR" ]

 

[root@web ~]# STR=hello.gif
[root@web ~]# SUFFIX=.gif
[root@web ~]# [ "${STR%$SUFFIX}" != "$STR" ] && echo "ends"
ends
[root@web ~]#

 

 

本文链接:http://codingstandards.iteye.com/blog/1187353   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头

下节内容:Bash字符串处理(与Java对照) - 18.格式化字符串

 

 

在mdk文件移植太多_XCubeClassB代码 的移植 前言ST在www.st.com官网上提供了针对IEC60730/IEC60335 ClassB认证的软件包。软件包分为三个,分别适用STM8,STM32(基于标准外设库)和STM32(基于HAL库):STM8-SafeCLASSB,STM32-CLASSB-SPL,X-CUBE-CLASSB。本文以X-CUBE-CLASSB为例介绍如何做代码的移植以及经常遇到的问题的解决办法。X-CUB... 阅读详情

相关推荐

C++上机——2023年“信息与未来”小学生编程思维展示官方题解

本次活动前三题是“根据问题描述实现代码”类型的问题,题解和示例代码由人工智能GPT-4直接阅读题面后生成――只要熟悉C++的语法和基本的编程、调试,即可解决p1、p2、p3。后三题有一定的算法性,我们给出命题思路和一些可能的解法。注意:本题解是面向指导教师的;指导教师在讲解时可以根据学生掌握程度进行调整。

qq_39127687的博客 2300

StringUtils工具类用法

/*1.字符串以prefix开始*/ StringUtils.startsWith("sssdf","");//结果是:true StringUtils.startsWith("sssdf","");//结果是:true StringUtils.startsWith("sssdf","s");//结果是:true StringUtils.startsWith("sssdf","ss");/

天堂地址不详 1011

实验室LIMS系统选型指南:从赛默飞到金现代,5大主流方案横评

本文深入分析了实验室LIMS系统选型的5大核心维度,包括架构选择、行业适配性、合规性设计、智能化程度和成本模型。通过对比赛默飞、金现代等主流厂商的优劣势,提供实战避坑策略,帮助实验室管理者做出明智决策,提升运营效率和合规水平。

plum99的博客 263

StringUtils的常见用法

前言 平常在项目中写代码经常用到这个类,今天刚好有时间就来深究下StringUtils到底有那些好用的方法 字面意思就大概知道这是一个操作String的工具类,下面我们打开源码一步一步的看下。 我初步看了下大概20多个。我们就从我们常用的一些入手。 StringUtils.isEmpty() 这是我们常用的一个判断字符串是否为空的一个方法 //对象不为空,或者不等于空字符 public static boolean isEmpty(@Nullable Object str) {

weixin_56227932的博客 2243

安卓设备安全相关技术

当前安卓设备使用的加密方案是基于Soc中一块可写入的rom实现,称为熔丝,其结果不可逆。 1、secureboot 2、AVB(Android Verify Boot) 3、dm-verity(device mapper) 4、keymaster 5、crypto 6、rpmb(replay private memory block) 7、TEE(Trust execute environment) 8、SELinux .........

weixin_43797328的博客 4847

数字IC设计避坑指南:为什么477条net的DRV问题无法修复?解析don‘t touch hierarchy的影响

本文深入解析了数字IC后端实现中,因“don't touch hierarchy”约束导致大量DRV违规(如477条net)无法自动修复的根本原因。文章以Cadence Innovus工具为例,详细阐述了DRV修复机制与设计保护约束的冲突原理,并提供了从日志定位、风险评估到分级解除约束的完整实战策略,帮助工程师有效解决此类设计收敛难题。

hope8的博客 294

java ignorecase_Spring中StringUtils.endsWithIgnoreCase方法的一个bug

由于项目的需要看了Spring框架StringUtils的源码,发现其中一个方法有点问题,以下是其方法的源代码:/*** Test if the given String ends with the specified suffix,* ignoring upper/lower case.* @param str the String to check* @param suffix the suf...

weixin_31133141的博客 3433

CRMEB小程序订阅消息实战指南:PHP配置与疑难排查

本文提供CRMEB小程序订阅消息的完整实战指南,重点解析PHP环境下的配置流程与常见错误解决方法。文章从订阅消息的核心规则讲起,详细拆解了从微信后台获取模板到CRMEB后台完成字段映射的关键步骤,并提供了从用户授权、缓存清除到服务器日志检查的逐层排查方案,帮助开发者高效打通消息通道,避免常见配置陷阱。

weixin_29022115的博客 322

java commons-lang3 包中 StringUtils 处理字符串的方法介绍

java 字符串操作大全,commons-lang3

gyk163的博客 2468

31、人工智能对神性观念的重塑与启示

本文探讨人工智能发展对神学与哲学中神性观念的深刻影响,通过奥古斯丁和爱任纽的神义论视角,分析AI系统的道德地位、自由意志类比、不可解释性与信任问题。文章提出‘可实例化的神性’这一新概念,认为在人与AI的互动中,神性在‘可能性矩阵’中得以实现。最后呼吁在技术进步的同时加强道德创造、可解释性研究与哲学神学反思,以实现人与技术的和谐发展。

xray4的博客 147

华为OD机试真题 新系统 2026-09-06 Python&JS【仓储货物有序出库】

某仓储中心有一个货物堆放区,货物编号按乱序堆叠存放,只能从顶部取货,管理员需要将货物按编号从小到大取出。

MISAYAONE的博客 131

java 判断包含6,Bash字符串处置(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)...

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)In JavaString.contains & String.indexOfString.contains方法只能判断是否包含某个子串,不能判断是否包含单个字符(当然能判断是否包含单个字符的子串)boolean ...

weixin_36255898的博客 162

抖音中动态生成msToken的方法

抖音中动态生成msToken的方法

sh_moranliunian的博客 2498

Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头

Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头 In Java String.startsWith  boolean     startsWith(String prefix)           测试此字符串是否以指定的前缀开始。  boolean     startsWith(String prefix, int toffset) ...

巨人站在肩膀上 334

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

BASH字符串处理:字符串分割为数组

http://codingstandards.iteye.com/blog/1164910 Bash字符串处理(与Java对照) - 25.字符串分割(成数组) In Java String.split  String[]     split(String regex)           根据给定的正则表达式的匹配来拆分此字符串。  String[]

zouyongjin的专栏 7764

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取

Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取 In Java 字符串替换  String     replace(char oldChar, char newChar)           返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。  String     replace(CharSe...

巨人站在肩膀上 634

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)

Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空) In Java 判断是否为null 是null if (s == null) {     // do something }   不是null if (s != null) {     // do something }   判断是否null或空串 是null或者空串:方法一 if...

巨人站在肩膀上 592

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)...

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法) In Java String.contains & String.indexOf String.contains方法只能判断是否包含某个子串,不能判断是否包含单个字符(当然能判断是否包含单个字符的子串) boolean     contains(CharSequence s)  ...

巨人站在肩膀上 398

java判断字符串是否包含某个字符串_Bash技巧:使用[[命令的 =~ 操作符判断字符串的包含关系...

在 Linux bash shell 中,可以使用 [[ 命令来进行判断。其中,可以使用 [[ 命令的 =~ 操作符来判断某个字符串是否包含特定模式。查看 man bash 对 [[ 命令的 =~ 操作符说明如下:An additional binary operator, =~, is available, with the same precedence as == and !=.When i...

weixin_39606575的博客 488

Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)...

Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较) In Java String.compareTo int     compareTo(String anotherString)           按字典顺序比较两个字符串。如果返回0,表示相等;<0表示小于;>0 表示大于。   int     compareToIgnor...

巨人站在肩膀上 808

Bash字符串处理(与Java对照) - 22.判断字符串是否数字串

Bash字符串处理(与Java对照) - 22.判断字符串是否数字串 In Java 用正则表达式匹配方式判断字符串是否数字串 String str = "1234"; if (str.matches("\\d+")) {     // matched, it's digit string }   In Bash 使用模式匹配(Pattern Matching)判...

巨人站在肩膀上 308

[Bash基础] 判断字符串相等

判断字符串相等 六种方式: test "$S1" = "$S2" [ "$S1" = "$S2" ] test "$S1" == "$S2" [ "$S1" == "$S2" ] [[ $S1 = $S2 ]] [[ $S1 == $S2 ]] Note: 在[[ ]]中,变量的引用可以不加双引号;这是与[ ]的不同之处。 判断字符串不相等 test "$S1" != "$S2" [ "$S1" != "$S2" ] [[ $S1 != $S2 ]] 也可以在判断字符串相等的基

mathemagics的博客 6057

Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)

Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等) In Java String.equals & String.equalsIgnoreCase boolean     equals(Object anObject)           比较此字符串与指定的对象。   boolean     equalsIgnoreCase(Strin...

巨人站在肩膀上 598

java 字符串截取替换_Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取...

Bash字符串处理(与Java对照) - 23.字符串替换、子串删除、子串截取In Java字符串替换?String ??? replace(char oldChar, char newChar)????????? 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。?String ??? replace(CharSequence target, C...

weixin_39601641的博客 169
上一篇: Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
下一篇: 导出MySQL数据库模式及数据的Bash脚本 真的很好用!
codingstandards
博客等级 码龄16年 12粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值