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.TextClass 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] = NothingConsole.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
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔