Solved

replace method

Posted on 2002-04-16
7
174 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 23
custom annotations 9 32
Java Options in Zimbra Server 2 46
Spring Framework HTTPSession management 1 23
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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now