Reputation: 11150
From the post String Builder Replace() is faster then string replace() in .NET language
I want to know if same answer is valid for java as well or not.
I checked and found
StringBuilder builder = ...;
builder = new StringBuilder(builder.toString().replace("from", "to"));
is inefficient as StringBuilder.toString()
is an expensive operation.
Why JAVA does not have replace
method in string builder class ?
Upvotes: 2
Views: 459
Reputation: 60046
From the documentation :
StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations
for that the StringBuilder::replace
is like :
public StringBuilder replace(int start, int end, String str)
Instead of String::replace
, because it is treated like an array and not a String
Upvotes: 3