天天看點

Bash字元串處理(與Java對照) - 17.判斷是否以另外的字元串結尾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 ]]

[[email protected] ~]# STR=hello.gif

[[email protected] ~]# SUFFIX=.gif

[[email protected] ~]# [[ $STR == *$SUFFIX ]] && echo "ends"

ends

[[email protected] ~]# 

使用[[ ]] 正規表達式比對來判斷是否以别的字元串結尾

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

在正規表達式中,$比對結尾。

[[email protected] ~]# STR=hello.gif

[[email protected] ~]# SUFFIX=.gif

[[email protected] ~]# [[ $STR =~ $SUFFIX$ ]] && echo "ends"

ends

[[email protected] ~]# 

例外:在上面的例子中,SUFFIX中包含點(.),而點(.)在正規表達式中能夠比對任意字元,如下所示:

[[email protected] ~]# STR=helloxgif

[[email protected] ~]# SUFFIX=.gif

[[email protected] ~]# [[ $STR =~ $SUFFIX$ ]] && echo "ends"

ends

[[email protected] ~]#

用case語句來判斷是否以别的字元串結尾

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

錯誤:case "$STR" in "*$SUFFIX") echo "ends"; esac

注意*不能寫在雙引号裡面,否則不靈。

[[email protected] ~]# STR=hello.gif

[[email protected] ~]# SUFFIX=.gif

[[email protected] ~]# case "$STR" in *"$SUFFIX") echo "ends"; esac

ends

[[email protected] ~]# case "$STR" in "*$SUFFIX") echo "ends"; esac

[[email protected] ~]#

用去尾法判斷是否以别的字元串結尾

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

[[email protected] ~]# STR=hello.gif

[[email protected] ~]# SUFFIX=.gif

[[email protected] ~]# [ "${STR%$SUFFIX}" != "$STR" ] && echo "ends"

ends

[[email protected] ~]#

本文連結:http://codingstandards.iteye.com/blog/1187353   (轉載請注明出處)

傳回目錄:Java程式員的Bash實用指南系列之字元串處理(目錄) 

上節内容:Bash字元串處理(與Java對照) - 16.判斷是否以另外的字元串開頭

下節内容:Bash字元串處理(與Java對照) - 18.格式化字元串