Link to home
Start Free TrialLog in
Avatar of msdixon
msdixon

asked on

JSP: replace method???

vb has the replace function, javascript has a replace method, but i can't find anything for java. there's a replace method but that's on a charachter level (i.e. replace 'a' with 'b'). but i want to replace '<' with '&lt;', and also a single quote with two single quotes.

what's the best way to accomplish this? regular expression?

thanks.
ASKER CERTIFIED SOLUTION
Avatar of Chad Smith
Chad Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smueller
smueller

I think the latest jdk1.4 beta has regular expressions built in.  But you can use this method to replace all occurrences of a specific character with a string:

String replaceCharacter(char oldChar, String replacement) {
     StringBuffer replacedText = new StringBuffer();
     int currentLocation = 0;
     int charIndex;
     while ((charIndex=text.indexOf(oldChar, currentLocation)) > -1) {
          replacedText.append(text.substring(currentLocation, charIndex)).append(replacement);
          currentLocation = charIndex + 1;
     }
     replacedText.append(text.substring(currentLocation, text.length()));
     return replacedText.toString();
}
I found the regular expression package for jdk1.4.  You can use the following code to replace all occurrences of a particular string with another string using the following code (this only works with jdk1.4):

Pattern p = Pattern.compile("<");
Matcher m = p.matcher("<html><body>'hello'</body></html>");
p = Pattern.compile("'");
m = p.matcher(m.replaceAll("&lt;"));
String result = m.replaceAll("''");
System.out.println(result);

This gives the following output (note that the input was "<html><body>'hello'</body></html>"):

&lt;html>&lt;body>''hello''&lt;/body>&lt;/html>
There are also third-party regex packages if you don't have control over your jdk version. One is at IBM alphaWorks, another is at Apache. But I would better write replace routine myself.

String replace(String where, String find, String insert ){
    int start = where.indexOf(find);
    if(start < 0){
        return where;
    }
    StringBuffer buffer = new StringBuffer(where.substring(0,start));
    buffer.append(insert);
    start += find.length();
    for(int pos = where.indexOf(find,start); pos>=0; pos = where.indexOf(find,start)){
        buffer.append(where.substring(start,pos))
        .append(insert);
        start = pos+find.length();
    }
    buffer.append(where.sustring(start));
    return buffer.toString();
}

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
Avatar of msdixon

ASKER

thanks all for the comments. i'll try and use each of them (hopefully today) and see which one works best.

thanks again.



on a side note, i didn't receive email notification that the comments were posted. is anyone else having the same problem?
Avatar of msdixon

ASKER

close...

i had to modify the code to:
     String strTemp = "<html>";
     StringBuffer sb = new StringBuffer(strTemp);
     int replaceLoc = strTemp.indexOf("<");
     sb.replace(replaceLoc, replaceLoc+ 1, "&lt;");
     out.println(sb);

the way you had it, it was trying to find the indexOf method of the StringBuffer class which doesn't exist. you pointed me in the right direction, and that's what i needed.

thanks.