Solved

replace method

Posted on 2002-04-16
7
178 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 92

Accepted Solution

by:
objects earned 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 140
printf performancy 11 86
running on tomcat not jboss eap 7.0 3 33
java mysql insert application 14 47
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

730 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