0

2つの文字列をコードと比較しようとしました:

public class MyClass
{
  public static void main(String args[])
  {
    String xhex="31 38 30 2E 32 35 35 2E 32 32 35 2E 31 32 33";
    String hex =  remspace(xhex).trim().toString();
    System.out.println(hex);
    String hex1="3138302E3235352E3232352E313233";
    System.out.println(hex1);
    if(hex.trim().equalsIgnoreCase(hex1.trim()))
    //if (hex.equals(hex1))
      {
        System.out.println("equals");
      }else
      {
        System.out.println("not equals");
      }
}

private static String remspace(String data)
    {
      String xdata = null;
      char c='\0';
      String hex = data.replace(' ',c);
      return hex;
    }
}

結果は次のとおりです。

3138302E3235352E3232352E313233
3138302E3235352E3232352E313233
not equals

ご覧のとおり、結果は同じように等しいですが、等号を使用して文字列を比較しようとすると、結果は等号ではありません。なぜ等しくないと見なされたのですか?

4

2 に答える 2

0

これは私のために働く:

public static String removeWhitespaces(String source){
    char[] chars = new char[source.length()];
    int numberOfNewlines = 0;
    for (int i = 0; i<chars.length; i++){
        if (source.charAt(i)==' ')
            numberOfNewlines++;
        else
            chars[i-numberOfNewlines]=source.charAt(i);
    }
    return new String(chars).substring(0, source.length()-numberOfNewlines);
}
于 2013-05-13T12:11:39.303 に答える