-3

String.equals() method in Java compares the backing arrays of two strings. So if the arrays are of different size same strings would not be equal.

Example:

char[] in = new char[50];
in[0] = 'K';
in[1] = 's';
in[2] = 'h';
in[3] = 'i';
in[4] = 't';
in[5] = 'i';
in[6] = 'z';
String s = new String(in);
System.out.println(s.equals("Kshitiz"));

Output:

false

9
  • Ideally you shouldn't but you need to when getting text as char[] from an API. Commented Apr 28, 2015 at 6:19
  • 2
    There are many ways to create a string from chars. If you want to pass an array, you should at least make it the right size. And char arrays are just like char values, they're not convenient and should rarely be used. Commented Apr 28, 2015 at 6:20
  • If the API returns a 50-long char array for a string of 7 chars, then the API is seriously f* up. Why would any sane API do that? Commented Apr 28, 2015 at 6:21
  • When getting char[] from an API method, making it a particular size isn't under my control. Commented Apr 28, 2015 at 6:21
  • 1
    @dystroy That's right. That's why I'm not editing the title. Someone else thinking along the same lines is likely to find this more useful and google for similar terms. Commented Apr 28, 2015 at 7:10

4 Answers 4

3

You should not be creating strings that don't hold what they should hold. Using a hack like trim to deal with bad objects is a bad practice.

Assuming you can't fix the API giving you badly sized char arrays, just count the not null chars before creating the string.

    char[] in = new char[50];
    in[0] = 'K';
    in[1] = 's';
    in[2] = 'h';
    in[3] = 'i';
    in[4] = 't';
    in[5] = 'i';
    in[6] = 'z';
    int length = 0;
    while (length<in.length && in[length]!=(char)0) length++;
    String s = new String(in, 0, length);
Sign up to request clarification or add additional context in comments.

Comments

0

equals() returns false because the two strings are not the same.

If you actually print the original string, then you will see the difference.

public static void main(String[] args) {
    char[] in = new char[50];
    in[0] = 'K';
    in[1] = 's';
    in[2] = 'h';
    in[3] = 'i';
    in[4] = 't';
    in[5] = 'i';
    in[6] = 'z';
    String s = new String(in);
    System.out.println(s.equals("Kshitiz"));
    System.out.println(s+ "X"); // new line
}

O/P :

false
Kshitiz                                X

You can see that the two strings are not the same.

Comments

0

Java's Strings are not null-terminated.

If you create Strings from a byte or character buffer, use a constructor that lets you specify the length.

new String(bytes, 0, 7, "ASCII");
new String(characters, 0, 7);

2 Comments

This would only work in this case. trim() is the way to go if you get char[] from somewhere else.
trim() also throws away stuff from the beginning. And spaces and tabs from the end. If someone gives you raw bytes or characters, they will also give you the length. Or have it null-terminated. You could then search for the 0 byte.
0

I added one more code line to your code to show what happen here.

    char[] in = new char[50];
    in[0] = 'K';
    in[1] = 's';
    in[2] = 'h';
    in[3] = 'i';
    in[4] = 't';
    in[5] = 'i';
    in[6] = 'z';
    String s = new String(in);
    System.out.println(s.equals("Kshitiz"));
    //just use following
    System.out.println(s); 

Now run this code. you can see s having \u0000 char just after Kshitiz

1 Comment

Not empty characters that would be space it's ASCII 0 or null.

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.