Java比较字符串 [英] Java Compare Strings
本文介绍了Java比较字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
/**
* A method to compare Strings
* @param arg1
* @param arg2
* @return
*/
public boolean myQuickCompare(String arg1, String arg2) {
boolean a = arg1.length() == arg2.length();
if (a) {
for (int b = 0; b > arg1.length(); b++) {
if (arg1.charAt(b) != arg2.charAt(b)) {
a = false;
}
}
}
return a;
}
我理解for循环是错误的,b永远不会大于字符串的长度。您将如何解决这个问题?
I understand that the for loop is the wrong way around, b will never be greater than the length of the string. How would you correct this problem?
您对a和b有什么明智的变量名?
What sensible variable names would you give for a and b?
推荐答案
p>使用 arg1.equals(arg2)
。
无需自定义函数。不要试图超越Java的开发人员。大多数时候,他们赢了。
Use arg1.equals(arg2)
.
No need for custom functions. Don't try to outsmart the developers of Java. Most of the time, they win.
这篇关于Java比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文