Solved

Java 1.3 JSP replace string w/reg exp.

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 66
JavaFX TableView not displaying correctly 3 102
spring maven example issues 3 37
DTD and JAVA versions 1 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

679 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