0

I have a String that contains hash (it was automatically generated by third party program) and I have another String that do not contain hash and I need to compare to the first String.

Method .equals() give false. How else I can compare them?

6
  • Never use == for strings (btw == is not a "method"). Commented Mar 11, 2014 at 13:01
  • post your input values. equals must work if both are same. Don't expect that a hash of string will be equal to the string. Commented Mar 11, 2014 at 13:01
  • What do you want to compare? the hashes or the strings? "equals" should work. Dont use ==. Commented Mar 11, 2014 at 13:01
  • I have two identical strings - first contains hash and second not. How can I compare them? Commented Mar 11, 2014 at 13:05
  • I want to compare if string values are identical. As I mentioned before - fisrt string contains hash, second - not. Therefore equals() method give false. How else can I do it? Commented Mar 11, 2014 at 13:09

3 Answers 3

2

If Methods == and .equals() give false, then both the String references are not pointing to neither the same object and nor meaningfully equivalent objects in heap. Adding to that, == should not be used to compare String literals

Sign up to request clarification or add additional context in comments.

Comments

0

String values are compared using equals(String anotherString). If that method returns false, the string values are simply not equal.

Check your strings for uppercase vs. lowercase, leading and trailing spaces and so on.

Comments

0

If I understood it correctly, there are two String to compare, one is the original String (s1) and the second String which contains the original String + its hash (s2) inserted by a 3rd party program. If so, the built-in equals() method will not return true. I would be creating my own implementation of equals() method to check this.

These two methods should do the trick, but I prefer the second one.

private static boolean equals(String s1, String s2) {
    String s1Hash = String.valueOf(s1.hashCode());
    return (s2.contains(s1) && s2.contains(s1Hash));
}

private static boolean equals2(String s1, String s2) {
    String s1Hash = String.valueOf(s1.hashCode());
    String s2NoHash = s2.replace(s1Hash,"");
    return (s1.equals(s2NoHash));
}

Hope this answers what you are looking for. Otherwise, a concrete example to describe the problem will be good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.