Last active
August 29, 2015 14:05
-
-
Save ipoletti/c58902bb9571c8cdc527 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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