• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with String.replace  RSS feed

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • 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: 598
1
  • Mark post as helpful
  • send pies
  • 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
 
Shan Accent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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: 598
1
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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: 598
1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
bur replaceAll("\\|","pipe") will work perfectly. try it... Haha.. [jumpingjoy]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!