Forums Register Login

How to remove '\0'

+Pie Number of slices to send: Send
Hi all,

Can any one tell me how to remove '\0' from the String.
+Pie Number of slices to send: Send
A short answer: you could use the String.replace() method to replace the 0 character with another character, or the replaceAll() method to replace it with an empty String.

But can you give me an example of what exactly you need to do? Where are you getting Strings with 0's in them?
+Pie Number of slices to send: Send
I was not asking about '0' in String, rather '\0' in the String.

while using '\0' terminated strings as parameters for the PreparedStatement in the JDBC query, I am getting this exception.

java.lang.IllegalArgumentException: \0 not allowed

I was asking on that context. I need remove '\0' not '0'

Thank you for your replay. Hope now you understand my question.
+Pie Number of slices to send: Send
A character with unicode value 0 is still 0, no matter how you write it.

OK, so, where are you getting these 0-terminated (or '\0'-terminated, if you insist I must type all those characters ) Strings from in the first place? They're not usual in Java, of course.
+Pie Number of slices to send: Send
I did it.

String str = resultSet.getString(1)

str = str.replace((char)0,(char)13);
+Pie Number of slices to send: Send
Ah, some broken C code is putting NUL terminators into the database! Go whack somebody on the head.
+Pie Number of slices to send: Send
using jakarta-commons-lang, you could also StringUtils.chop(string) to remove the last character (no matter what it is)
+Pie Number of slices to send: Send
 

Originally posted by Sanju Thomas:
I did it.

String str = resultSet.getString(1)

str = str.replace((char)0,(char)13);



That doesn't remove the null character, but does replace it with the character of unicode 13.

To really just remove it, use

str = str.replaceAll("\\u00", "");
+Pie Number of slices to send: Send
Thank You Guys.
My, my, aren't you a big fella. Here, have a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12230 times.
Similar Threads
Removing session
removing the trailing space of a string only
Getting an InterruptedException after closing Frame
LDAP entry
how to make a collection as unmodifiable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:22:22.