Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
来源:互联网 发布:bf风是什么意思淘宝 编辑:程序博客网 时间:2024/04/25 12:43
From: http://codingstandards.iteye.com/blog/1187353
In Java
String.endsWith
oolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
StringUtils.endsWith & StringUtils.endsWithIgnoreCase & StringUtils.endsWithAny
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
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
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 ~]#
- Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
- Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
- Bash字符串处理(与Java对照)
- Bash字符串处理(与Java对照)
- Bash字符串处理(与Java对照) - 18.格式化字符串
- 判断字符串是否以指定的字符串结尾
- 判断是否以指定的字符串开头或结尾
- Java判断字符串的开始与结尾
- 判断某个字符串是否以某个字符串开头或结尾
- Bash字符串处理(与Java对照) - 19.查找字符的位置
- Bash字符串处理(与Java对照) - 20.查找子串的位置
- 判断字符串是否包含另外的字符串
- Oracle自定义函数,判断一个以逗号拼接的字符串是否完全包含另外一个字符串,与拼接顺序无关
- JS判断变量是否以某个字符串结尾
- 检查一个字符串是否以指定的字符串结尾
- 检查一个字符串是否以指定的字符串结尾
- 判断字符串的开始与结尾(常用于文件结尾格式的判断)
- python-12-如何判断字符串a是否以字符串b开头或结尾(改变文件权限)?
- CListCtrl控件中InsertItem和SettItemtext函数的用法简介
- eCos中的线程与同步
- 面试题
- 给站里面的兄弟姐妹们拜年了!
- MP4音频解码信息(转帖加注释)
- Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
- Setting up ccache on android 4.0.3
- C# 浅拷贝与深拷贝区别
- 事务和锁(SQLite)
- Linux 如何用vim命令把编辑文件的几行内容拷贝到一个新文件
- 解决IE6下无法显示png图片问题
- 如何查看类对象数组中类成员变量的全部值,类成员变量也是数组。
- MacOS上port命令使用方法
- Bash字符串处理(与Java对照) - 18.格式化字符串