Lovable Bastard
Join Date: Dec 2005
Location: Boston-ish
|
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 |
Not a tame lion...
Join Date: May 2004
Location: Narnia
|
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 |
Veteran Member
Join Date: Nov 2005
|
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 |
Lovable Bastard
Join Date: Dec 2005
Location: Boston-ish
|
Quote:
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 |
Senior Member
Join Date: Nov 2004
|
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 |
Banging the Bottom End
Join Date: Jun 2004
|
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 |
Veteran Member
Join Date: Nov 2005
|
Quote:
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 |
|
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 |