Well, you can write a loop:
public static void replaceAll(StringBuilder builder, String from, String to) {
int index = builder.indexOf(from);
while (index != -1) {
builder.replace(index, index + from.length(), to);
index += to.length(); // Move to the end of the replacement
index = builder.indexOf(from, index);
}
}
Note that in some cases it may be faster to use lastIndexOf, working from the back. I suspect that's the case if you're replacing a long string with a short one - so when you get to the start, any replacements have less to copy. Anyway, this should give you a starting point.
Well, you can write a loop:
public static void replaceAll(StringBuilder builder, String from, String to) {
int index = builder.indexOf(from);
while (index != -1) {
builder.replace(index, index + from.length(), to);
index += to.length(); // Move to the end of the replacement
index = builder.indexOf(from, index);
}
}
Note that in some cases it may be faster to use lastIndexOf, working from the back. I suspect that's the case if you're replacing a long string with a short one - so when you get to the start, any replacements have less to copy. Anyway, this should give you a starting point.
You could use Pattern/Matcher. From the Matcher javadocs:
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
java - How to use replaceAll() method in StringBuffer? - Stack Overflow
java - Replacing individual characters in a StringBuffer - Stack Overflow
Dart Doesn't Have StringBuffer.replace, Is There a Workaround?
java - How can I remove characters from a StringBuffer? - Stack Overflow
Videos
There is no such method on StringBuffer. Perhaps the following will help:
StringBuffer str1 = new StringBuffer("whatever");
// to get a String result:
String str2 = str1.toString().replaceAll(regex, substr);
// to get a StringBuffer result:
StringBuffer str3 = new StringBuffer(str2);
This solution is a bit more optimized, as it does not create new strings or string buffers. Suppose you want to replace str1 with str2.
StringBuffer strBuf = new StringBuffer("yourString");
int startIndex = strBuf.indexOf(str1);
while(startIndex != -1){
strBuf.replace(start, start + str1.length(), str2);
start = strBuf.indexOf(str1, start + str1.length());
}
With this method, you find the index of each occurance of str1 in your buffer and replace it one by one with str2. The method indexOf(String str, int index), returns the index of the first occurance of str starting from index.
Is there a way to find and replace a substring inside a StringBuffer without creating a new String object? Java and dotnet have this. Could there be a pub.dev package anyone knows about?
This might work for you:
StringBuffer s=...
for(char c: passcode.toCharArray()){
int index=-1;
while((index=s.indexOf(c))!=-1){
s.deleteCharAt(index);
}
}
You didn't indicate which version of java you are using but in 7 StringBuffer does have a replace method.
replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String.
Combine this with the indexOf method to replace all occurrences.
int ndx = alphabet.indexOf(String.valueOf(replacedLetter), 0);
while (ndx > -1) {
alphabet.replace(ndx, ndx + 1, "");
ndx = alphabet.indexOf(String.valueOf(replacedLetter), ndx);
}
Well, you can use a regular expression to find the cases where "Milan" isn't followed by "Vasic":
Milan(?! Vasic)
and replace that by the full name:
String.replaceAll("Milan(?! Vasic)", "Milan Vasic")
The (?!...) part is a negative lookahead which ensures that whatever matches isn't followed by the part in parentheses. It doesn't consume any characters in the match itself.
Alternatively, you can simply insert (well, technically replacing a zero-width match) the last name after the first name, unless it's followed by the last name already. This looks similar, but uses a positive lookbehind as well:
(?<=Milan)(?! Vasic)
You can replace this by just " Vasic" (note the space at the start of the string):
String.replaceAll("(?<=Milan)(?! Vasic)", " Vasic")
You can try those things out here for example.
Another option:
"My name is Milan, people know me as Milan Vasic"
.replaceAll("Milan Vasic|Milan", "Milan Vasic"))