`

Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)

阅读更多

Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)

In Java

String.equals & String.equalsIgnoreCase

boolean     equals(Object anObject)
          比较此字符串与指定的对象。

 

boolean     equalsIgnoreCase(String anotherString)
          将此 String 与另一个 String 进行比较,不考虑大小写。

 

 

if (s1.equals(s2)) {

}

注意:一定要保证s1 != null,否则会抛出异常。

 

StringUtils.equals & StringUtils.equalsIgnoreCase

在Apache Commons Lang中的StringUtils类提供了equals和equalsIgnoreCase静态方法,它的好处是两个字符串都可以是null。

org.apache.commons.lang.StringUtils equals方法 写道
public static boolean equals(String str1, String str2)

Compares two Strings, returning true if they are equal.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils.equals(null, null) = true
StringUtils.equals(null, "abc") = false
StringUtils.equals("abc", null) = false
StringUtils.equals("abc", "abc") = true
StringUtils.equals("abc", "ABC") = false

Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case sensitive, or both null
 
org.apache.commons.lang.StringUtils equalsIgnoreCase方法 写道
public static boolean equalsIgnoreCase(String str1, String str2)

Compares two Strings, returning true if they are equal ignoring the case.

nulls are handled without exceptions. Two null references are considered equal. Comparison is case insensitive.

StringUtils.equalsIgnoreCase(null, null) = true
StringUtils.equalsIgnoreCase(null, "abc") = false
StringUtils.equalsIgnoreCase("abc", null) = false
StringUtils.equalsIgnoreCase("abc", "abc") = true
StringUtils.equalsIgnoreCase("abc", "ABC") = true


Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case insensitive, or both null
 

In Bash

判断字符串相等

格式1:test "$S1" = "$S2"

格式2:[ "$S1" = "$S2" ]

格式3:test "$S1" == "$S2"

格式4:[ "$S1" == "$S2" ]

格式5:[[ $S1 = $S2 ]]

格式6:[[ $S1 == $S2 ]]

 

在Bash中关于=和==的说法

man bash 写道
string1 == string2
True if the strings are equal. = may be used in place of == for strict POSIX compliance.

 

[root@web ~]# S1=Hello
[root@web ~]# S2=Hello
[root@web ~]# test "$S1" = "$S2"  && echo "equals"
equals
[root@web ~]# [ "$S1" = "$S2" ]  && echo "equals"   
equals
[root@web ~]# test "$S1" == "$S2"  && echo "equals"
equals
[root@web ~]# [ "$S1" == "$S2" ]  && echo "equals" 
equals
[root@web ~]# [[ "$S1" = "$S2" ]]  && echo "equals"
equals
[root@web ~]# [[ "$S1" == "$S2" ]]  && echo "equals"
equals
[root@web ~]#

[root@web ~]# S1=Hello
[root@web ~]# S2=hello
[root@web ~]# test "$S1" = "$S2"  && echo "equals"
[root@web ~]# [ "$S1" = "$S2" ]  && echo "equals"   
[root@web ~]# test "$S1" == "$S2"  && echo "equals"
[root@web ~]# [ "$S1" == "$S2" ]  && echo "equals" 
[root@web ~]# [[ "$S1" = "$S2" ]]  && echo "equals"
[root@web ~]# [[ "$S1" == "$S2" ]]  && echo "equals"
[root@web ~]#

 

 

判断字符串不相等

格式1:test "$S1" != "$S2"

格式2:[ "$S1" != "$S2" ]

格式3:[[ $S1 != $S2 ]]

注意在[[ ]]中,变量的引用可以不加双引号,这是与[ ]的不同之处。

也可以在判断字符串相等的基础上加上逻辑非(!)可以得到更多方法,比如:[ ! "$S1" == "$S2" ]

 

 

[root@web ~]# S1=Hello
[root@web ~]# S2=Hello
[root@web ~]# test "$S1" != "$S2" && echo "not equals"
[root@web ~]# [ "$S1" != "$S2" ] && echo "not equals"     
[root@web ~]# [[ $S1 != $S2 ]] && echo "not equals"   
[root@web ~]# S1=Hello
[root@web ~]# S2=hello
[root@web ~]# test "$S1" != "$S2" && echo "not equals"
not equals
[root@web ~]# [ "$S1" != "$S2" ] && echo "not equals"
not equals
[root@web ~]# [[ $S1 != $S2 ]] && echo "not equals"
not equals
[root@web ~]#

 

比较字符串是否相等,不区分大小写

采用shopt命令启用不区分大小写匹配模式,只在case语句和[[ ]]中有效。

man bash: shopt 写道
shopt
    Toggle the values of variables controlling optional shell behavior.

-s Enable (set) each optname.
-u Disable (unset) each optname.

nocasematch
    If set, bash matches patterns in a case-insensitive fashion when performing matching while exe-
cuting case or [[ conditional commands.
 

[root@web ~]# shopt -s nocasematch
[root@web ~]# [ "$S1" = "$S2" ] && echo "equals ignore case"
[root@web ~]# [[ "$S1" = "$S2" ]] && echo "equals ignore case"
equals ignore case
[root@web ~]# case "$S1" in "$S2") echo "equals ignore case"; esac
equals ignore case
[root@web ~]# shopt -u nocasematch
[root@web ~]# [[ "$S1" = "$S2" ]] && echo "equals ignore case"   
[root@web ~]# case "$S1" in "$S2") echo "equals ignore case"; esac
[root@web ~]#

 

包装成一个函数

equals_ignore_case(){
    shopt -s nocasematch
    [[ $1 == $2 ]]
    local rc=$?
    shopt -u nocasematch
    return $rc
}
 

[root@web ~]# equals_ignore_case "Hello" "hello" && echo "equals ignore case"
equals ignore case
[root@web ~]# equals_ignore_case "Hello" "Qello" && echo "equals ignore case"
[root@web ~]# equals_ignore_case "Hello" "hello" && echo "equals ignore case"
equals ignore case
[root@web ~]# equals_ignore_case "Hello" "helLo" && echo "equals ignore case"  
equals ignore case
[root@web ~]#

 

如果其中一个字符串是常量,可以采用 [[ ]] 中 == 的模式匹配来进行。(不用shopt开启不区分大小写)

比如:[[ $S1 == [Hh][Ee][Ll][Ll][Oo] ]]

麻烦的地方就是后面那个匹配模式要每个字符提供大小写的形式。

 

[root@web ~]# S1=Hello
[root@web ~]# [[ $S1 == [Hh][Ee][Ll][Ll][Oo] ]] && echo "equals ignore case"    
equals ignore case
[root@web ~]# S1=HeLlo
[root@web ~]# [[ $S1 == [Hh][Ee][Ll][Ll][Oo] ]] && echo "equals ignore case"
equals ignore case
[root@web ~]#

 

还可以采用case语法结构。

比如:case "$S1" in [Hh][Ee][Ll][Ll][Oo]) echo "equals ignore case";; esac

 

[root@web ~]# S1=hEllo
[root@web ~]# case "$S1" in [Hh][Ee][Ll][Ll][Oo]) echo "equals ignore case";; esac
equals ignore case
[root@web ~]# S1=hellO
[root@web ~]# case "$S1" in [Hh][Ee][Ll][Ll][Oo]) echo "equals ignore case";; esac
equals ignore case
[root@web ~]#

 

如果比较的双方都是变量,上面的技巧就无法做到了。

下面的方法是将两个字符串都变成大写的形式(使用tr命令),然后进行判断。

[ "$(echo "$S1" | tr [a-z] [A-Z])" == "$(echo "$S2" | tr [a-z] [A-Z])" ]

 

[root@web ~]# S1=Hello
[root@web ~]# S2=helLo
[root@web ~]# [ "$(echo "$S1" | tr [a-z] [A-Z])" == "$(echo "$S2" | tr [a-z] [A-Z])" ] && echo "equals ignore case"
equals ignore case
[root@web ~]#

 

 

 

本文链接:http://codingstandards.iteye.com/blog/1175608   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符

下节内容:Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)

 

 

4
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics