读书人

Bash字符串处置(与Java对照) - 14.判

发布时间: 2012-07-16 15:44:59 作者: rapoo

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)In JavaString.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

判断是否包含某个字符


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实用指南系列之字符串处理(目录)?

上节内容:Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)

下节内容:Bash字符串处理(与Java对照) - 15.计算子串出现的次数

?

?

读书人网 >操作系统

热点推荐