Java 比较字符串

发布于 2024-11-24 04:49:40 字数 482 浏览 2 评论 0原文

 /**
 * 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 永远不会大于字符串的长度。您将如何纠正这个问题?

你会给 a 和 b 起什么合理的变量名?

 /**
 * 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;
}

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?

What sensible variable names would you give for a and b?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉宸 2024-12-01 04:50:36

有几点:

  1. 当您比较字符串是否相等时,请使用 .equals 方法。 == 用于比较对象引用并查看它们是否引用同一个实例。 .equals 实际上比较 String 对象内的字符。
  2. 即使您按照自己的方式做事(这是不正确的),for 循环也应该如下所示

    for (int b = 0; b < arg1.length(); b++)

A couple of things:

  1. When you compare strings for equality, use the .equals method. == is used to compare object references and see if they refer to the same instance. .equals actually compares the characters inside the String object.
  2. Even if you do things your way (which is incorrect), the for loop should look like this

    for (int b = 0; b < arg1.length(); b++)

哆啦不做梦 2024-12-01 04:50:34

一个=>结果
b => current

检查任一参数是否为 null 会很有帮助。

a => result
b => current

It would be helpful to check if either of arguments is null.

凡尘雨 2024-12-01 04:50:33

I always use StringUtils.compare ( Apache Commons ). This handles the null case for either String argument as well.

孤君无依 2024-12-01 04:50:32

我知道 for 循环是错误的,b 永远不会大于字符串的长度。您将如何解决这个问题?

直接使用Stringequals()

您会给 a 和 b 起什么合理的变量名称?

a 可能是 result

b 可能是 index


这里是 equals() 的实现>来自开放jdk 7

 public boolean equals(Object anObject) {
 1014           if (this == anObject) {
 1015               return true;
 1016           }
 1017           if (anObject instanceof String) {
 1018               String anotherString = (String)anObject;
 1019               int n = count;
 1020               if (n == anotherString.count) {
 1021                   char v1[] = value;
 1022                   char v2[] = anotherString.value;
 1023                   int i = offset;
 1024                   int j = anotherString.offset;
 1025                   while (n-- != 0) {
 1026                       if (v1[i++] != v2[j++])
 1027                           return false;
 1028                   }
 1029                   return true;
 1030               }
 1031           }
 1032           return false;
 1033       }

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?

use equals() of String directly

What sensible variable names would you give for a and b?

a may be result

b may be index


Here is the implementation of equals() from open jdk 7

 public boolean equals(Object anObject) {
 1014           if (this == anObject) {
 1015               return true;
 1016           }
 1017           if (anObject instanceof String) {
 1018               String anotherString = (String)anObject;
 1019               int n = count;
 1020               if (n == anotherString.count) {
 1021                   char v1[] = value;
 1022                   char v2[] = anotherString.value;
 1023                   int i = offset;
 1024                   int j = anotherString.offset;
 1025                   while (n-- != 0) {
 1026                       if (v1[i++] != v2[j++])
 1027                           return false;
 1028                   }
 1029                   return true;
 1030               }
 1031           }
 1032           return false;
 1033       }
无风消散 2024-12-01 04:50:27

使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文