diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 7eb33fa104..921522a34e 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -228,6 +228,33 @@ public final class String implements Comparable { return -1; } + public String replace(char oldChar, char newChar) { + if (data instanceof char[]) { + char[] buf = new char[length]; + for (int i=0; i < length; i++) { + if (charAt(i) == oldChar) { + buf[i] = newChar; + } else { + buf[i] = charAt(i); + } + } + return new String(buf, 0, length, false); + } else { + byte[] buf = new byte[length]; + byte[] orig = (byte[])data; + byte oldByte = (byte)oldChar; + byte newByte = (byte)newChar; + for (int i=0; i < length; i++) { + if (orig[i+offset] == oldByte) { + buf[i] = newByte; + } else { + buf[i] = orig[i+offset]; + } + } + return new String(buf, 0, length, false); + } + } + public String substring(int start) { return substring(start, length); } diff --git a/classpath/java/lang/StringBuffer.java b/classpath/java/lang/StringBuffer.java index d216370464..66780d24c7 100644 --- a/classpath/java/lang/StringBuffer.java +++ b/classpath/java/lang/StringBuffer.java @@ -60,6 +60,11 @@ public class StringBuffer { return this; } + public synchronized StringBuffer delete(int start, int end) { + sb.delete(start, end); + return this; + } + public synchronized StringBuffer deleteCharAt(int i) { sb.deleteCharAt(i); return this; @@ -73,6 +78,11 @@ public class StringBuffer { return sb.length(); } + public synchronized StringBuffer replace(int start, int end, String str) { + sb.replace(start, end, str); + return this; + } + public synchronized void setLength(int v) { sb.setLength(v); } diff --git a/classpath/java/lang/StringBuilder.java b/classpath/java/lang/StringBuilder.java index 731da9169d..02e8510c52 100644 --- a/classpath/java/lang/StringBuilder.java +++ b/classpath/java/lang/StringBuilder.java @@ -148,6 +148,15 @@ public class StringBuilder { return insert(i, new String(new char[] { c }, 0, 1, false)); } + public StringBuilder delete(int start, int end) { + int numChars = end - start; // Do not delete end itself + while (numChars > 0) { + deleteCharAt(start); + numChars--; + } + return this; + } + public StringBuilder deleteCharAt(int i) { if (i < 0 || i >= length) { throw new IndexOutOfBoundsException(); @@ -185,6 +194,12 @@ public class StringBuilder { return this; } + public StringBuilder replace(int start, int end, String str) { + delete(start, end); + insert(start, str); + return this; + } + public int length() { return length; }