• 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

problem with String.replace

 
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to take in a string and replace all ocurances of | with pipe.

The problem is that text.replaceAll("|","pipe"); replaces all chars with the string pipe.
Has anyone had a problem like this before.

Tony
 
Tony Evans
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What it is doing is inserting pipe between each char.

so the sting "THIS" will be "pipeTpipeHpipeIpipeSpipe"

Thanks for any help

Tony
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can do it the following way...

code:
----

String str = "A|B";
str = str.replaceAll("[|]","pipe");

output:
--------
ApipeB.

try it...
---------------------
Shan
 
Tony Evans
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Shan worked a treat, wot do the [] around the recxp do then, or they similar to using / as a escape char.

I guess | is seen as a special charector.

Tony
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replaceAll does not perform a string replace, it performs a regular expression replace. And the pipe symbol is a special character in regular expressions, which does not mean its literal self. The javadocs for java.util.regex.Pattern have some of the gory details on that.

You need to escape the symbol by a preceding backslash; then it is taken literally: replaceAll("\|","pipe")
[ August 24, 2005: Message edited by: Ulf Dittmer ]
 
Tony Evans
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Shan worked a treat, wot do the [] around the recxp do then, or they similar to using / as a escape char.

I guess | is seen as a special charector.

Tony
 
Shan Accent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai Ulf Dittmer , replaceAll("\|","pipe") won't work. It will give a syntax error. try it... He..
 
Shan Accent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bur replaceAll("\\|","pipe") will work perfectly. try it... Haha.. [jumpingjoy]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"shan", please read (or at least browse) the JavaRanch display name rule here.

Disply names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Your display name used to be valid, not it is not. Please change it back immediately, since accounts with invalid display names get deleted.

thanks,
Dave.

PS browsing your posts it appears you provide much needed help, it would be a shame if your account were deleted
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic