Implements String.delete(char oldChar, String newChar), StringBuffer and

StringBuilder delete(int startIndex, int endIndex), and
replace (int startIndex, int endIndex, String replacementString)
This commit is contained in:
Eric Scharff
2007-10-29 15:07:36 -06:00
parent 78ee14fff5
commit bcd2c75f41
3 changed files with 52 additions and 0 deletions

View File

@ -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;
}