• Post Reply Bookmark Topic Watch Topic
  • New Topic

replaceAll and hex characters  RSS feed

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all,

I'm trying transmorgify this Perl subroutine which replaces various characters and groups of characters with either nulls or spaces



and came up with this



to be called like this


which compiles but doesn't seem to replace anything.

Any suggestions/pointers/constructive criticism is appreciated.

TIA,

Still-learning Steve
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. When you call replaceAll, it does not modify the original String but instead returns the result. You need to use this result:
Now, when you call this method, you should assign the result value to your original String reference:

By the way, shouldn't you use "" instead of null for the replacement values? I thought that null would cause a runtime exception.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying!

Yes you are correct on both counts. It's working now. Thanks and Happy Holidays!


CASE CLOSED

Still-learning Steve
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a several issues with your code.

First, in the line
the comment does not reflect regular expression. The regex says replace anything outside of space to tilde inclusive with a space.

Second, as far as I can see, the 3 separate replaceAll() statements that replace characters with a space can be merged to just one.

Third, the use of hex values makes your regular expressions just about unreadable.

Fourth, the 'null' replacement values will cause a NPE !



 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!