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
判断是否包含某个字符
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
判断是否包含另外的子串
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* ]]
注意:*不能引起来,否则不灵。
[root@jfht ~]# STR=123456789
[root@jfht ~]# SUB=456
[root@jfht ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
contains
[root@jfht ~]# SUB=4568
[root@jfht ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
[root@jfht ~]# SUB="1 2"
[root@jfht ~]# [[ "$STR" == *"$SUB"* ]] && echo contains;
[root@jfht ~]# [[ "$STR" == *$SUB* ]] && echo contains;
[root@jfht ~]# STR="1 2 3"
[root@jfht ~]# [[ "$STR" == *$SUB* ]] && echo contains;
contains
[root@jfht ~]#
特殊情况:以某子串开头。
[[ $STR == $SUB* ]]
特殊情况:以某子串结尾。
[[ $STR == *$SUB ]]
[root@jfht ~]# STR=123456789
[root@jfht ~]# SUB=123
[root@jfht ~]# [[ "$STR" == $SUB* ]] && echo "starts";
starts
[root@jfht ~]# [[ "$STR" == *$SUB ]] && echo "ends";
[root@jfht ~]# SUB=789
[root@jfht ~]# [[ "$STR" == $SUB* ]] && echo "starts";
[root@jfht ~]# [[ "$STR" == *$SUB ]] && echo "ends";
ends
使用正则表达式匹配方式确定是否包含子串
[[ $STR =~ .*$SUB.* ]]
注:.*是不必要的,可写成
[[ $STR =~ $SUB ]]
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# [[ "$STR" =~ .*$SUB.* ]] && echo contains
contains
[root@jfht ctmw]# [[ "$STR" =~ $SUB ]] && echo contains
contains
使用case语句来确定是否包含子串
case "$STR" in *$SUB*) echo contains; ;; esac
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# case "$STR" in *$SUB*) echo contains; ;; esac
contains
[root@jfht ctmw]#
使用字符串替换来实现是否包含子串
if [ "$STR" != "${STR/$SUB/}" ]; then echo contains; fi
解读:如果将字符串STR中的SUB子串删除掉之后,不与STR相等,就表明STR中包含SUB串。
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# if [ "$STR" != "${STR/$SUB/}" ]; then echo contains; fi
contains
[root@jfht ctmw]#
使用grep来实现是否包含子串
if echo "$STR" | grep -q "$SUB"; then echo contains; fi
if grep -q "$SUB" <<<"$STR"; then echo contains; fi
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# if echo "$STR" | grep -q "$SUB"; then echo contains; fi
contains
[root@jfht ctmw]# if grep -q "$SUB" <<<"$STR"; then echo contains; fi
contains
[root@jfht ctmw]#
使用expr match来实现是否包含子串
if [ "$(expr match "$STR" ".*$SUB.*")" != "0" ]; then echo contains; fi
[root@jfht ctmw]# STR=123456789
[root@jfht ctmw]# SUB=456
[root@jfht ctmw]# if [ "$(expr match "$STR" ".*$SUB.*")" != "0" ]; then echo contains; fi
contains
[root@jfht ctmw]#
本文链接:http://codingstandards.iteye.com/blog/1181490 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)