【问题标题】:Comparing Two strings with different order比较两个不同顺序的字符串
【发布时间】:2015-04-07 02:47:48
【问题描述】:

我有一个格式为

的字符串

PosAttributes: FN,CT,ST;

现在当我计算一些功能时,我得到一个字符串

PosAttributes1: FN,ST,CT;

现在虽然两个字符串都暗示了同样的事情,如果使用下面的 equal 函数,它会返回 false。我知道两个刺不一样,但语义是一样的。我该怎么办?

PosAttributes.equals(PosAttributes);

【问题讨论】:

  • 您的字符串是否可能包含重复的属性?如果是这样,是否应该将具有不同重复值的字符串视为不同?例如,FN,ST,CT,CT 是否等于 CT,FN,ST,ST

标签: java string compare


【解决方案1】:

由于字符串由逗号分隔,您可以使用 String.split 来给出

String arr[] = PosAttributes.split (",");
String arr2[] = PosAttributes1.split (",");

那么您只需要遍历第一个数组,确保所有元素都在第二个数组中。还要检查尺寸是否相同。

【讨论】:

  • 我可以确保在一个 if 语句中语义相同吗?
  • 创建一个为您执行此操作的方法。
【解决方案2】:

您需要分解每个字符串的各个部分,并将它们存储在某种集合中 - 这是一种没有顺序的结构,或者顺序不影响equals 方法的结果。我会写一个这样的方法。

private static Set<String> attributeSet(String input) {
    String[] attributes = input.split(",");
    return new HashSet<String>(Arrays.asList(attributes));
}

假设分隔符是逗号,这会将字符串分成多个段。然后它使用标准技巧将生成的数组转换为HashSet,这是Set的常用类型。

然后当我想比较两个字符串时,我可以写类似

if (attributeSet(string1).equals(attributeSet(string2))) {
    // ...
}

【讨论】:

  • 这种方法只有在字符串不能包含重复项时才有效。例如“FN、CT、ST、FN”不是有效字符串。否则,该字符串将被视为等于“FN,CT,ST”。由 OP 决定这是否是一个问题。
  • 是的,这是真的。听起来我应该在问题下提出评论。谢谢@deyur。
【解决方案3】:

因此假设示例文本是全文,您需要删除; 字符,因为这会改变String 的内容,将String 拆分为, 字符,对结果数组并比较它们,比如...

String[] v1 = "FN,CT,ST;".replace(";", "").split(",");
String[] v2 = "FN,ST,CT;".replace(";", "").split(",");
Arrays.sort(v1);
Arrays.sort(v2);
System.out.println(Arrays.equals(v1, v2));

哪个输出true

我可能想做的是编写一个返回 String 的排序数组的方法,复制所有常见功能...

public static String[] sorted(String value) {
    String[] v1 = value.replace(";", "").split(",");
    Arrays.sort(v1);
    return v1;
}

然后你可以简单地调用它,这样你就可以进行一次类似的比较......

System.out.println(Arrays.equals(sorted("FN,CT,ST;"), sorted("FN,ST,CT;")));

下一步可能是编写一个返回true 的方法,它调用sortedArrays.equals 以使其更容易...

System.out.println(isEqual("FN,CT,ST;", "FN,ST,CT;"));

但是,我会留给你;)

【讨论】:

    【解决方案4】:

    您可以覆盖 equal 方法或对两个字符串进行排序然后比较它们。

    【讨论】:

      【解决方案5】:

      我现在在工作中也有同样的要求,想避免使用列表进行评估。

      我所做的是检查要比较的两个字符串是否长度相等——这意味着它们可能相同,只是顺序不同。

      现在删除比较字符串中的主字符串中的逗号分隔字符串。如果主字符串的输出为空,则意味着两者是精确的数学。请看下面的伪代码(我没有粘贴实际代码,因为它有一些公司特定的信息):

      private static boolean isStringCombinationEqual(final String main, final String compare)
      {
          boolean result = false;
          String modifiedMain = main;
      
          // if not the same length, then cannot possibly be same combination in different order
          if (main.length() == compare.length())
          {
              final String[] compareArr = // split compare using delimiter
      
              for (int i = 0; i < compareArr.length; i++)
              {
                  if (i > 0)
                  {
                      modifiedMain = // replace delimiter with empty string
                  }
      
                  modifiedMain = // replace compareArr[0] with empty string
              }
      
              if (//modifiedMain is empty)
              {
                  result = true;
              }
          }
      
          return result;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-08
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多