3

I have a C-style string encoded as an array of characters in Java, but I would like to convert this array to a Java String. I tried using the matching constructor call,

String toRet = new String(new char[]{'B','A','D','\0', 'G', 'A', 'R', 'B', 'A', 'G', 'E'});
System.out.println(Arrays.toString(toRet.toCharArray()));

But the result is incorrect, and in fact oddly buggy. Here's what the above code outputs:

[B, A, D,

And here's what I want

[B, A, D]

I'm running on openJdk6 on Ubuntu. I haven't tested the above code on other VM's.

2
  • You perhaps can have a look to [this:][1] [1]: stackoverflow.com/questions/1487023/… Commented Oct 23, 2012 at 9:52
  • Thanks, that's a useful related question. Commented Oct 23, 2012 at 9:56

3 Answers 3

1

There is no need for a String to get involved here. Just copy into a new array that is one char shorter than your input array. The method to use for this task is Arrays.copyOf.

The reason your output is buggy is because strings in Java have nothing to do with null-terminators. Your first line of code creates a string whose last character is the null-character.

Response to your updated question

If you have garbage following the null-char, you can use new String(inputArray), then find the null-char with String.indexOf('\0') and use that in a String.substring operation to cut out the unneeded part. However, it would be still simpler (from the time/space complexity perspective) to just iterate over the array to locate the null-char and then use Arrays.copyOf with that index as cutoff point.

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

2 Comments

Modified question to clarify.
O.K. It, so it takes a few steps, but that's fine. I'll mark this as an accepted answer whenever the system lets me.
0

You can use trim() to remove space chars:

System.out.println(Arrays.toString(toRet.trim().toCharArray()));

2 Comments

This works when the null terminator character ends the string, but not when there are garbage characters after the null terminator I want to ignore. Cool trick though, I didn't know trim() also removes \0's at the end.
trim() defines whitespace as \u0000 to \u0020 inclusive - it does not use the Unicode definition of whitespace, nor Character.isWhitespace.
0

To confuse people that have to maintain your code, you could write:

char[] dta = new char[]{'B','A','D','\0', 'G', 'A', 'R', 'B', 'A', 'G', 'E'};

String string = (dta[0] == '\0') ? "" : (new String(dta)).split("\0")[0];
System.out.println(string.toCharArray());

2 Comments

This doesn't work. "\0".split("\0")[0] throws IndexOutOfBoundsException
Indeed, so what should the resulting String contain in that case? To avoid another line of code: String string = dta[0] == '\0' ? "" : (new String(dta)).split("\0")[0];

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.