Solved

replace method

Posted on 2002-04-16
7
173 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

759 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

24 Experts available now in Live!

Get 1:1 Help Now