wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to remove '\0' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to remove Watch "How to remove New topic
Author

How to remove '\0'

Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Hi all,

Can any one tell me how to remove '\0' from the String.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
I did it.

String str = resultSet.getString(1)

str = str.replace((char)0,(char)13);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Ah, some broken C code is putting NUL terminators into the database! Go whack somebody on the head.
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
using jakarta-commons-lang, you could also StringUtils.chop(string) to remove the last character (no matter what it is)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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", "");


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
Thank You Guys.
 
 
subject: How to remove '\0'