Solved

Java 1.3 JSP replace string w/reg exp.

Posted on 2009-04-06
5
732 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

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
Printing to a specific printer tray (HP 806dn printer) 3 83
Java Message handling in Service Layer 3 57
maven example error 3 52
session migration servlets 2 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 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

26 Experts available now in Live!

Get 1:1 Help Now