一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

string类equals方兴方法null和""的比较区别

时间:2011-06-07 编辑:简简单单 来源:一聚教程网

string类equals方兴方法null和""的比较区别

'声明

Public Shared Function Equals ( _
 a As String, _
 b As String, _
 comparisonType As StringComparison _
) As Boolean
参数
a
类型:System.String
要比较的第一个字符串,或 Nothing。

b
类型:System.String
要比较的第二个字符串,或 Nothing。

comparisonType
类型:System.StringComparison
枚举值之一,用于指定比较的规则。

返回值
类型:System.Boolean
如果 a 参数的值等于 b 参数的值,则为 true;否则为 false。

String.equals()方法比较的是字符串的内容,使用equals(...)方法会对字符串中的所有字符一个接一个地进行比较,如果完全相等那么返回true。 在这种情况下全部字符串都是相同的,所以当字符串s0 与s1 或s2 比较时我们得到的返回值均为true 。运算符“==”比较的是String 实例的引用。在这种情况下很明显s0 和s1 并不是同一个String 实例,但s0 和s2 是同一个

public class StringTest {
    public static void main(String[] args) {
        String str = null;
        String str2 = "";
        
        System.out.println(""" == str: " + "".equals(str));
        System.out.println(""" == str2: " + "".equals(str2));
        
        try {
            System.out.println(str.equals(""));
        } catch (NullPointerException e) {
            System.err.println("Caught Exception: " + e);
        }
    }
}


运行结果如下:
"" == str: false
"" == str2: true
Caught Exception: java.lang.NullPointerException
在Java中String有很多特别的地方, 推荐比较字符串为空时使用这种方式:
"".equals(str)


下面的示例使用 Equals 方法的三个版本确定 String 对象与 StringBuilder 对象是否相等。

 

Sample for String.Equals(Object)
'            String.Equals(String)
'            String.Equals(String, String)
Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb As New StringBuilder("abcd")
      Dim str1 As [String] = "abcd"
      Dim str2 As [String] = Nothing
      Dim o2 As [Object] = Nothing

      Console.WriteLine()
      Console.WriteLine(" *  The value of String str1 is '{0}'.", str1)
      Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString())

      Console.WriteLine()
      Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.")
      Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb))

      Console.WriteLine()
      Console.WriteLine("1b) String.Equals(Object). Object is a String.")
      str2 = sb.ToString()
      o2 = str2
      Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2)
      Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2))

      Console.WriteLine()
      Console.WriteLine(" 2) String.Equals(String)")
      Console.WriteLine(" *  The value of String str2 is '{0}'.", str2)
      Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2))

      Console.WriteLine()
      Console.WriteLine(" 3) String.Equals(String, String)")
      Console.WriteLine("    Is str1 equal to str2?: {0}", [String].Equals(str1, str2))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
' *  The value of String str1 is 'abcd'.
' *  The value of StringBuilder sb is 'abcd'.
'
'1a) String.Equals(Object). Object is a StringBuilder, not a String.
'    Is str1 equal to sb?: False
'
'1b) String.Equals(Object). Object is a String.
' *  The value of Object o2 is 'abcd'.
'    Is str1 equal to o2?: True
'
' 2) String.Equals(String)
' *  The value of String str2 is 'abcd'.
'    Is str1 equal to str2?: True
'
' 3) String.Equals(String, String)
'    Is str1 equal to str2?: True

热门栏目