• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 22649
126
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: 22649
126
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 !



 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic