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 '<', and also a single quote with two single quotes.
what's the best way to accomplish this? regular expression?
thanks.
what's the best way to accomplish this? regular expression?
thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>'he llo'</body ></html>") ;
p = Pattern.compile("'");
m = p.matcher(m.replaceAll("&l t;"));
String result = m.replaceAll("''");
System.out.println(result) ;
This gives the following output (note that the input was "<html><body>'hello'</body ></html>") :
<html><body>''hello' '</body ></html >
Pattern p = Pattern.compile("<");
Matcher m = p.matcher("<html><body>'he
p = Pattern.compile("'");
m = p.matcher(m.replaceAll("&l
String result = m.replaceAll("''");
System.out.println(result)
This gives the following output (note that the input was "<html><body>'hello'</body
<html><body>''hello'
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.substri ng(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.substr ing(start, pos))
.append(insert);
start = pos+find.length();
}
buffer.append(where.sustri ng(start)) ;
return buffer.toString();
}
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
String replace(String where, String find, String insert ){
int start = where.indexOf(find);
if(start < 0){
return where;
}
StringBuffer buffer = new StringBuffer(where.substri
buffer.append(insert);
start += find.length();
for(int pos = where.indexOf(find,start);
buffer.append(where.substr
.append(insert);
start = pos+find.length();
}
buffer.append(where.sustri
return buffer.toString();
}
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
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?
thanks again.
on a side note, i didn't receive email notification that the comments were posted. is anyone else having the same problem?
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, "<");
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.
i had to modify the code to:
String strTemp = "<html>";
StringBuffer sb = new StringBuffer(strTemp);
int replaceLoc = strTemp.indexOf("<");
sb.replace(replaceLoc, replaceLoc+ 1, "<");
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.
String replaceCharacter(char oldChar, String replacement) {
StringBuffer replacedText = new StringBuffer();
int currentLocation = 0;
int charIndex;
while ((charIndex=text.indexOf(o
replacedText.append(text.s
currentLocation = charIndex + 1;
}
replacedText.append(text.s
return replacedText.toString();
}