Solved

Java 1.3 JSP replace string w/reg exp.

Posted on 2009-04-06
5
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…
Suggested Courses

734 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