比较硒中的两个字符串
我需要从页面的行中获取值,
我的代码是
String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals(bname1,"HDFC");
if(bname1=="HDFC") {
System.out.println("Bank name is:"+bname1);
} else {
System.out.println("Bank name not found");
}
System.out.println(bname1);
结果: 找不到银行名称 HDFC
我的银行名称是“VIJAYA” 但是当我与“bname1”和“VIJAYA”进行比较时,结果会是负数吗? 我如何比较这些字符串 请帮助我...
I need to take value from row from page
my code is
String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals(bname1,"HDFC");
if(bname1=="HDFC") {
System.out.println("Bank name is:"+bname1);
} else {
System.out.println("Bank name not found");
}
System.out.println(bname1);
Result:
Bank name not found
HDFC
My bank name is "VIJAYA"
But when i compare to "bname1" and "VIJAYA",RESULT will be negative?
How can i compaire these strings
pls help me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 中不能使用
==
来比较字符串。所做的只是测试两个对象在内存中是否具有相同的地址/是否是相同的实例。使用.equals()
代替,例如:if(bname1.equals("HDFC"))
...或者最好是:
if("HDFC".equals (bname1))
哪个更好,因为如果 'bname1' 为
null
,它不会崩溃。You cannot compare strings in Java using
==
. All that does is test to see if the two objects have the same address in memory/are the same instance. Use.equals()
instead, like:if(bname1.equals("HDFC"))
...or preferably:
if("HDFC".equals(bname1))
Which is better because it won't crash if 'bname1' is
null
.您的原始代码还有其他一些问题。当您调用assertEquals()时,它告诉编译器检查两个值是否相等,如果条件不成立则停止执行代码。因此,如果断言失败,则在assertEquals()语句之后找到的打印语句将不会被执行。相反,您应该尝试执行以下操作。
如果您这样编写语句,则assertEquals函数的第一个参数将成为断言失败时显示的错误消息。
Your original code has some other issues as well. When you call assertEquals() that tells the compiler to check if the two values are equal and then stop executing code if the condition is not true. Because of this the print statements found after your assertEquals() statement will not get executed if the assertion fails. Instead you should try to do something like the following.
If you write your statement like this the first parameter of the assertEquals function will become the error message displayed when the assertion fails.
试试这个..
Try this..
我相信它会对你有帮助
I'm sure It will help you
您可以简单地使用如下断言:
这里 ExpectedString 是预期的字符串,ActualString 是原始字符串值。
You can simply use assertions like:
Here ExpectedString is a string that is expected and ActualString is original String value.