• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replaceAll and hex characters

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic