String s1=str”;equals() 区分大小写(小写或大写):
字符串 s2=str”;
if(s1.equals(s2))
System.out.println(两个字符串相等”);
String s1=str”;在这种情况下,它返回 false.
字符串 s2=STR”;
if(s1.equals(s2))
System.out.println(两个字符串相等”);
String s1=str”;
字符串 s2=STR”;
if(s1.equalsIgnoreCase(s2))
System.out.println(对忽略大小写的敏感度。两个字符串相等);
if(string1”.length()==string2”.length())
System.out.println(string1 和 string2 大小相等”);
else if(string1”.length()>”string2.length())
System.out.println(”string1 更长);
else
System.out.println(string2 更长”);
new String(test”).equals(new String(test”));//-->true==”运算符测试对象的相等性,这里有一系列需要避免的错误:
String s1=trial”;串联返回 true,因为它是在编译过程中调用的,这与在运行时调用的 substring() 和 replace() 不同,并且将创建一个不等于等式另一侧的新对象><。
字符串 s2=trial”;
//它们是同一个对象
s1==s2;-->false
//它们不是同一个对象
s1 == test”;-->false
//它们引用同一个对象
test” == test”;-->true
//串联被视为单个对象
test” == your”+t”;-->true
//substring() 方法生成一个新对象
test” == 1test”.substring(1);-->false
//replace() 方法生成一个新对象
test” == Test”.replace(T”, t”);-->false
Please disable your ad blocker and refresh the window to use this website.