PDA

View Full Version : Trimming Null Values from Strings (Java)


Kraetos
2008-03-25, 02:45
A program I'm writing has a method that manipulates a string. At the end, the string it creates has null values in it because it was created from an array which had null values in it.

Problem is, you can't trim nulls. Spaces, sure, Nulls, no. So whenever I call for the length of my new string, it is too long; there are trailing nulls. This can be easily reproduced:


public static void main(String[] args)
{
char nullArray[] = new char[3];

nullArray[0] = 'r';

String charString = new String(nullArray);

charString.trim();

System.out.println(charString.length());
}


The result, of course, is 3. You would expect it to be 1, but the trim method doesn't get the nulls, so now I have null values trailing my strings. How can I get rid of them?

(Obviously I could use a for loop to move all the desired values into a new, correctly sized array, and then put that array into a new string, but there is probably a more efficient way, yes?)

EDIT: Brad, are you actually using Bitstream Vera Sans Mono as the code font, or am I seeing things?

EDIT2: Yes, yes you are.

AsLan^
2008-03-25, 03:35
You should build the string with a loop as you suggeested, something like this:


StringBuilder str = new StringBuilder();

for (int i = 0; i < nullArray.length; i++) {

if (nullArray[i] != null) {

str.append(nullArray[i]);
}
}

colivigan
2008-03-25, 07:37
Or use a StringBuffer instead of a char array.


public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append('r');
String charString = sb.toString();
System.out.println(charString.length());
}

Kraetos
2008-03-25, 14:03
Or use a StringBuffer instead of a char array.


public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append('r');
String charString = sb.toString();
System.out.println(charString.length());
}

Noted for future reference. Thanks :)

On that note, I decided to add some more code to my method to manipulate the "dirty" string rather than make a new method to clean it. This isn't code I plan to reuse any time soon.

Enki
2008-03-25, 14:20
Noted for future reference. Thanks :)

On that note, I decided to add some more code to my method to manipulate the "dirty" string rather than make a new method to clean it. This isn't code I plan to reuse any time soon.

Famous last words! Take the 5 minutes to fix the problem forever and you can happily forget about it. Your future you's will undoubtedly thank you for it.

bassplayinMacFiend
2008-03-25, 14:59
Why use a char array to begin with? Just use a String object to begin, and concatenate (use '+') new characters to the existing String object.

colivigan
2008-03-25, 15:09
Why use a char array to begin with? Just use a String object to begin, and concatenate (use '+') new characters to the existing String object.
That will probably work just as well.

In the old days when the world was young and Java was slow, we were taught not to do this. A String is immutable, so every concatenation is forced to create a new String object. The StringBuffer is more efficient.

Nowadays, unless you're doing a gazillion concatenations, it probably doesn't matter.