Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to remove '\0'

 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can any one tell me how to remove '\0' from the String.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it.

String str = resultSet.getString(1)

str = str.replace((char)0,(char)13);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, some broken C code is putting NUL terminators into the database! Go whack somebody on the head.
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using jakarta-commons-lang, you could also StringUtils.chop(string) to remove the last character (no matter what it is)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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", "");
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic