?
Solved

replace method

Posted on 2002-04-16
7
Medium Priority
?
180 Views
Last Modified: 2010-03-31
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
Comment
Question by:dgeorgio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6946201
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
 
LVL 92

Expert Comment

by:objects
ID: 6946373
Seen a bug already :)

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

    pos = i + a.length() + 1;
0
 

Author Comment

by:dgeorgio
ID: 6946638
Am i able to search for carridge returns and linefeeds as a string as per your example?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 6946654
Should just be able to use control characters, like:

replace(s, "\r\n", "<p>);
0
 

Author Comment

by:dgeorgio
ID: 6946670
kewl, thought so. I will give it a go tonight.
ta muchly
0
 

Author Comment

by:dgeorgio
ID: 6947784
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
 
LVL 92

Expert Comment

by:objects
ID: 6948965
Happy to hear it solved your problem :-)

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

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question