当前位置:首页 > 开发 > 操作系统 > 正文

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

发表于: 2011-10-09   作者:codingstandards   来源:转载   浏览:
摘要: Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾 In Java String.endsWith oolean     endsWith(String suffix)           测试此字符串是否以指定的后缀结束。   Stri

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.格式化字符串

 

 

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号