• Post Reply Bookmark Topic Watch Topic
  • New Topic

String replaceAll  RSS feed

 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a String str = "Hello||World|Nice|To||Hear";

I want to replace every "||" (two pipes repeating simultaneously) with
"| |"(pipe whitepace and pipe)

I tried to use

But this replaces every "|" with a "| ". Is there a way I can only replace "||" with "| |";
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way is to just use the String replace method that replaces a char sequence with another char sequence:

str.replace("||", "| |");

If you want to "do it yourself," you'll probably want to use a StringBuffer since you'll be "mutating." You can use a loop to find the index of the first "||", then replace that substring (from index to index plus 2) with the desired substring ("| |").


[ November 30, 2004: Message edited by: marc weber ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
The easy way is to just use the String replace method that replaces a char sequence with another char sequence:

str.replace("||", "| |");


That would be the easiest way if such a method existed, which as far as I can tell doesn't.

The next easy way is to use the proper regex:

str.replaceAll("\\|\\|", "| |");

(This is untested and therefore might not work as written, but you should get the idea.)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
That would be the easiest way if such a method existed...

The method is new as of 1.5:
replace(CharSequence target, CharSequence replacement).
 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
Thanks for your response, i found out that unfortunately my application does not support Java 1.4 and 1.5. I can only use 1.3 and found this solution. Please let me know if there is a better way.



//Martin
[ November 30, 2004: Message edited by: Martin Lira ]
 
Sainath Veepuri
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

Originally posted by Martin Lira:
Hey Guys,
Thanks for your response, i found out that unfortunately my application does not support Java 1.4 and 1.5. I can only use 1.3 and found this solution. Please let me know if there is a better way.



StringBuffer sbuff = new StringBuffer();
int iLength = str.length();
int iLen = iLength - 1;

for(int i=0; i<iLength; i++){
if(i<iLen && str.charAt(i)
== '|' && str.charAt(i+1) == '|'){
sbuff.append("| ");
}
else
sbuff.append(str.charAt(i));

}




=====>> I made it a bit better, performace wise..
- SAi
[ November 30, 2004: Message edited by: Sainath Veepuri ]
[ November 30, 2004: Message edited by: Sainath Veepuri ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!