天天看点

Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)...Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)

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.计算子串出现的次数