User Name
Password
AppleNova Forums » Programmer's Nook »

Trimming Null Values from Strings (Java)


Register Members List Calendar Search FAQ Posting Guidelines
Trimming Null Values from Strings (Java)
Thread Tools
Kraetos
Lovable Bastard
 
Join Date: Dec 2005
Location: Boston-ish
 
2008-03-25, 01: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:

Code:
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.

Logic, logic, logic. Logic is the beginning of wisdom, Valeris, not the end.
  quote
AsLan^
Not a tame lion...
 
Join Date: May 2004
Location: Narnia
 
2008-03-25, 02:35

You should build the string with a loop as you suggeested, something like this:

Code:
StringBuilder str = new StringBuilder(); for (int i = 0; i < nullArray.length; i++) { if (nullArray[i] != null) { str.append(nullArray[i]); } }
  quote
colivigan
Veteran Member
 
Join Date: Nov 2005
 
2008-03-25, 06:37

Or use a StringBuffer instead of a char array.

Code:
public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append('r'); String charString = sb.toString(); System.out.println(charString.length()); }
  quote
Kraetos
Lovable Bastard
 
Join Date: Dec 2005
Location: Boston-ish
 
2008-03-25, 13:03

Quote:
Originally Posted by UncleJohn View Post
Or use a StringBuffer instead of a char array.

Code:
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.

Logic, logic, logic. Logic is the beginning of wisdom, Valeris, not the end.
  quote
Enki
Senior Member
 
Join Date: Nov 2004
 
2008-03-25, 13:20

Quote:
Originally Posted by Kraetos View Post
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.
  quote
bassplayinMacFiend
Banging the Bottom End
 
Join Date: Jun 2004
 
2008-03-25, 13: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.
  quote
colivigan
Veteran Member
 
Join Date: Nov 2005
 
2008-03-25, 14:09

Quote:
Originally Posted by bassplayinMacFiend View Post
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.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling & Running Java? (on OS X) MagSafe Programmer's Nook 6 2006-05-06 09:39
Very disappointed by my G5 and Java... nicolas_bol Apple Products 69 2006-04-29 18:15
Get your Java updates! Only 45 mb. Quagmire Apple Products 31 2005-09-15 19:46


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 20:05.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova