Bash字元串處理(與Java對照) - 14.判斷是否包含另外的字元串(多達6種方法)
In Java
String.contains & String.indexOf
String.contains方法隻能判斷是否包含某個子串,不能判斷是否包含單個字元(當然能判斷是否包含單個字元的子串)
boolean contains(CharSequence s)
當且僅當此字元串包含 char 值的指定序列時,才傳回 true。
使用String.indexOf方法判斷是否包含某個字元
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
if (s.contains(c)) {
}
使用String.indexOf方法判斷是否包含某個子串
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
if (str.indexOf(sub) >= 0) {
// do something
}
使用String.matches方法判斷是否包含子串,注意正規表達式元字元的轉義
boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
if (str.matches(".*"+sub+".*")) {
// do something
}
StringUtils.contains
判斷是否包含某個字元
org.apache.commons.lang.StringUtils contains方法 寫道 public static boolean contains(String str, char searchChar)
Checks if String contains a search character, handling null. This method uses String.indexOf(int).
A null or empty ("") String will return false.
StringUtils.contains(null, *) = false
StringUtils.contains("", *) = false
StringUtils.contains("abc", 'a') = true
StringUtils.contains("abc", 'z') = false
Parameters:
str - the String to check, may be null
searchChar - the character to find
Returns:
true if the String contains the search character, false if not or null string input
判斷是否包含另外的子串
org.apache.commons.lang.StringUtils contains方法 寫道 public static boolean contains(String str, String searchStr)
Checks if String contains a search String, handling null. This method uses String.indexOf(String).
A null String will return false.
StringUtils.contains(null, *) = false
StringUtils.contains(*, null) = false
StringUtils.contains("", "") = true
StringUtils.contains("abc", "") = true
StringUtils.contains("abc", "a") = true
StringUtils.contains("abc", "z") = false
Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
Returns:
true if the String contains the search String, false if not or null string input
In Bash
是否包含子串(推薦方式)
[[ $STR == *$SUB* ]]
[[ $STR == *$SUB* ]]
注意:*不能引起來,否則不靈。
[[email protected] ~]# STR=123456789
[[email protected] ~]# SUB=456
[[email protected] ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
contains
[[email protected] ~]# SUB=4568
[[email protected] ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
[[email protected] ~]# SUB="1 2"
[[email protected] ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
[[email protected] ~]# [[ "$STR" == *$SUB* ]] && echo contains;
[[email protected] ~]# STR="1 2 3"
[[email protected] ~]# [[ "$STR" == *$SUB* ]] && echo contains;
contains
[[email protected] ~]#
特殊情況:以某子串開頭。
[[ $STR == $SUB* ]]
特殊情況:以某子串結尾。
[[ $STR == *$SUB ]]
[[email protected] ~]# STR=123456789
[[email protected] ~]# SUB=123
[[email protected] ~]# [[ "$STR" == $SUB* ]] && echo "starts";
starts
[[email protected] ~]# [[ "$STR" == *$SUB ]] && echo "ends";
[[email protected] ~]# SUB=789
[[email protected] ~]# [[ "$STR" == $SUB* ]] && echo "starts";
[[email protected] ~]# [[ "$STR" == *$SUB ]] && echo "ends";
ends
使用正規表達式比對方式确定是否包含子串
[[ $STR =~ .*$SUB.* ]]
注:.*是不必要的,可寫成
[[ $STR =~ $SUB ]]
[[email protected] ctmw]# STR=123456789
[[email protected] ctmw]# SUB=456
[[email protected] ctmw]# [[ "$STR" =~ .*$SUB.* ]] && echo contains
contains
[[email protected] ctmw]# [[ "$STR" =~ $SUB ]] && echo contains
contains
使用case語句來确定是否包含子串
case "$STR" in *$SUB*) echo contains; ;; esac
[[email protected] ctmw]# STR=123456789
[[email protected] ctmw]# SUB=456
[[email protected] ctmw]# case "$STR" in *$SUB*) echo contains; ;; esac
contains
[[email protected] ctmw]#
使用字元串替換來實作是否包含子串
if [ "$STR" != "${STR/$SUB/}" ]; then echo contains; fi
解讀:如果将字元串STR中的SUB子串删除掉之後,不與STR相等,就表明STR中包含SUB串。
[[email protected] ctmw]# STR=123456789
[[email protected] ctmw]# SUB=456
[[email protected] ctmw]# if [ "$STR" != "${STR/$SUB/}" ]; then echo contains; fi
contains
[[email protected] ctmw]#
使用grep來實作是否包含子串
if echo "$STR" | grep -q "$SUB"; then echo contains; fi
if grep -q "$SUB" <<<"$STR"; then echo contains; fi
[[email protected] ctmw]# STR=123456789
[[email protected] ctmw]# SUB=456
[[email protected] ctmw]# if echo "$STR" | grep -q "$SUB"; then echo contains; fi
contains
[[email protected] ctmw]# if grep -q "$SUB" <<<"$STR"; then echo contains; fi
contains
[[email protected] ctmw]#
使用expr match來實作是否包含子串
if [ "$(expr match "$STR" ".*$SUB.*")" != "0" ]; then echo contains; fi
[[email protected] ctmw]# STR=123456789
[[email protected] ctmw]# SUB=456
[[email protected] ctmw]# if [ "$(expr match "$STR" ".*$SUB.*")" != "0" ]; then echo contains; fi
contains
[[email protected] ctmw]#
本文連結:http://codingstandards.iteye.com/blog/1181490 (轉載請注明出處)
傳回目錄:Java程式員的Bash實用指南系列之字元串處理(目錄)
上節内容:Bash字元串處理(與Java對照) - 13.字元串數組連接配接(以指定分隔符合并)
下節内容:Bash字元串處理(與Java對照) - 15.計算子串出現的次數