Bash字符串处理(与Java对照)
2012-04-25 16:14
316 查看
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
In Java
String.contains & String.indexOf
String.contains方法只能判断是否包含某个子串,不能判断是否包含单个字符(当然能判断是否包含单个字符的子串)当且仅当此字符串包含 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* ]]
注意:*不能引起来,否则不灵。
[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; fiif 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]#
/article/4053516.html
相关文章推荐
- Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
- Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
- Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
- Bash字符串处理(与Java对照) - 18.格式化字符串
- Bash字符串处理(与Java对照) - 20.查找子串的位置
- Bash字符串处理(与Java对照) - 19.查找字符的位置
- Java程序员的Bash实用指南系列之字符串处理(目录)
- java.sql.Exception:setString 只能处理少于 32766 个字符的字符串
- Java字符串处理String、StringBuilder、StringBuffer类效率分析
- Java字符串处理
- 黑马程序员--java 基础之线程与字符串处理类-----04
- JAVA环境使用正则表达式处理字符串
- Java:处理字符串
- mysql和java中时间对照以及处理方法
- java 字符串空格处理
- BASH 中的字符串处理
- Java 字符串的处理
- Java利用gson处理json字符串
- Java对象创建方式及JVM对字符串处理
- Bash字符串的处理