Solved

Java 1.3 JSP replace string w/reg exp.

Posted on 2009-04-06
5
729 Views
Last Modified: 2012-05-06
I need to come up with a good way to search through a string and replaceAll certain characters with another string.

If the string contains either of the following: & ' "
I need to replace all & with "and" and replace all single/double quote with "quote".

In my code below, how do I modify it so it searches for either of the 3 characters, without looping 3 times, one for each char.
String source = "mel & laura & cindy's car";

String input = "";

String pattern = "&";
 

if (source!=null)

{

        final int len = pattern.length();

        StringBuffer sb = new StringBuffer();

        int found = -1;

        int start = 0;
 

        while( (found = source.indexOf(pattern, start) ) != -1) {

            sb.append(source.substring(start, found));

            sb.append(replace);

            start = found + len;

        }
 

        sb.append(source.substring(start));
 

        input =  sb.toString();

}

Open in new window

0
Comment
Question by:galaxy573
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24082227
easier to use replaceAll(), why not use that

0
 

Author Comment

by:galaxy573
ID: 24082286
Java 1.3 does not have replaceAll() unfortunately.
0
 
LVL 92

Expert Comment

by:objects
ID: 24082323
sorry didn't notice you were using 1.3.

why is looping a problem? You're already looping (for each instance of the string), so looping for all possibles strings seems ok. In fact it would seem the cleanest

for (int=0; i
0
 

Author Comment

by:galaxy573
ID: 24082606
I'm getting the syntax error:

array required but java.lang.String found

for this line:

String pattern = patterns[i];
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24082618
patterns should be an array of strings

String[] patterns = new String[] {"&", "\""};

you'll also need an array of replacements

String[] replaces = new String[] {"and", "quote"};

which you will use :

            sb.append(replaces[i]);

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now