AG
2001-02-21, 22:09
Skulle vilja använda följande replace-funktion på min jsp sida, importera eller nåt.
class MyStrings {
public static String replace (String target, String from, String to) {
// target is the original string
// from is the string to be replaced
// to is the string which will used to replace
int start = target.indexOf (from);
if (start==-1) return target;
int lf = from.length();
char [] targetChars = target.toCharArray();
StringBuffer buffer = new StringBuffer();
int copyFrom=0;
while (start != -1) {
buffer.append (targetChars, copyFrom, start-copyFrom);
buffer.append (to);
copyFrom=start+lf;
start = target.indexOf (from, copyFrom);
}
buffer.append (targetChars, copyFrom, targetChars.length-copyFrom);
return buffer.toString();
}
}
Hade funderingar på att göra nåt med bönor, med är en som sagt osäker.
Själva variabelsättnigen skulle nog vara nåt i stil med:
String variabel = "allt som finns i världen";
String out = MyStrings.replace(variabel, "världen", "europa");
Behöer alltså hjälp med hur jag skall inkludera detta.
Andreas
class MyStrings {
public static String replace (String target, String from, String to) {
// target is the original string
// from is the string to be replaced
// to is the string which will used to replace
int start = target.indexOf (from);
if (start==-1) return target;
int lf = from.length();
char [] targetChars = target.toCharArray();
StringBuffer buffer = new StringBuffer();
int copyFrom=0;
while (start != -1) {
buffer.append (targetChars, copyFrom, start-copyFrom);
buffer.append (to);
copyFrom=start+lf;
start = target.indexOf (from, copyFrom);
}
buffer.append (targetChars, copyFrom, targetChars.length-copyFrom);
return buffer.toString();
}
}
Hade funderingar på att göra nåt med bönor, med är en som sagt osäker.
Själva variabelsättnigen skulle nog vara nåt i stil med:
String variabel = "allt som finns i världen";
String out = MyStrings.replace(variabel, "världen", "europa");
Behöer alltså hjälp med hur jag skall inkludera detta.
Andreas