• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

replace method

Is there a method available that i can use that will find all the carridge returns and replace them with a <br> and detect all double (2x) carridge returns and replace them with a <p>?

what i want to do is detect these in a block of text that i extract from my database and format it when it is rendered to the jsp. Pretty much the same way this page is formatted :D.

0
dgeorgio
Asked:
dgeorgio
  • 4
  • 3
1 Solution
 
objectsCommented:
Doesn't exist, you'll need to write it yourself. Something like the following, though I haven't tested it so they'res sure to be a bug or two:

public String replaceAll(String s, String a, String b)
{
   StringBuffer result = new StringBuffer();
   int pos = 0;
   int i;
   while (-1!=(i=s.indexOf(a, pos)))
   {
     result.append(s.substring(pos, i));
     result.append(b);
     pos = i + b.length() + 1;
   }
   result.append(s.substring(pos));
   return result.toString();
}
0
 
objectsCommented:
Seen a bug already :)

Should be adding length of a to pos, not b.

    pos = i + a.length() + 1;
0
 
dgeorgioAuthor Commented:
Am i able to search for carridge returns and linefeeds as a string as per your example?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
Should just be able to use control characters, like:

replace(s, "\r\n", "<p>);
0
 
dgeorgioAuthor Commented:
kewl, thought so. I will give it a go tonight.
ta muchly
0
 
dgeorgioAuthor Commented:
Thanx muchly object. Works a treat.

I have also found another solution that you may be interested in. String taglib has a replace tag that does the same as your supplied solution. might be another one to keep in mind.

http://jakarta.apache.org/taglibs/doc/string-doc/index.html#reference

<str:replace replace="" with=""></str:replace>

0
 
objectsCommented:
Happy to hear it solved your problem :-)

http://www.objects.com.au
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now