Skip to content

Instantly share code, notes, and snippets.

@ipoletti
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipoletti/c58902bb9571c8cdc527 to your computer and use it in GitHub Desktop.
Save ipoletti/c58902bb9571c8cdc527 to your computer and use it in GitHub Desktop.
public class StringBuilderWrapper implements CharSequence {
private StringBuilder sb;
public StringBuilderWrapper(String s) {
this.sb = new StringBuilder(s);
}
public int length() {
return sb.length();
}
public char charAt(int index) {
return sb.charAt(index);
}
public CharSequence subSequence(int start, int end) {
return sb.subSequence(start, end);
}
public StringBuilderWrapper replace(CharSequence a, CharSequence b) {
String aa = a.toString();
String bb = b.toString();
int indexOf;
int from = 0;
while((indexOf= sb.indexOf(aa, from)) >= 0) {
sb.replace(indexOf, indexOf + aa.length() , bb);
from = indexOf + bb.length(); // to avoid recursion problem
}
return this;
}
public String toString() {
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment