3

I'm using StringBuilder, instead of String, in my code in effort to make the code time-efficient during all that parsing & concatenation.

But when i look into its source, the substring() method of AbstractStringBuilder and thus StringBuilder is returning a String and not a StringBuilder.

What would be the idea behind this ?

Thanks.

7
  • 7
    Well, the name is substring, not substringbuilder. Commented Nov 12, 2013 at 16:25
  • 1
    You might be looking for subSequence(int start, int end). (P.S., why do you insist on using bold instead of code formatting for code?) Commented Nov 12, 2013 at 16:28
  • @TedHopp Hopp nope, thats not the Q. that and substring are next to one another in the code. Commented Nov 12, 2013 at 16:29
  • @Kayaman what are you talking about. there hasn't been a "substringbuilder" in this text since the creation of Earth. Commented Nov 12, 2013 at 16:31
  • How would you expect to use a StringBuilder that was returned from substring()? Commented Nov 12, 2013 at 16:32

2 Answers 2

6

The reason the substring method returns an immutable String is that once you get a part of the string inside your StringBuilder, it must make a copy. It cannot give you a mutable "live view" into the middle of StringBuilder's content, because otherwise you would run into conflicts of which changes to apply to what string.

Since a copy is to be made anyway, it might as well be immutable: you can easily make it mutable if you wish by constructing a StringBuilder around it, with full understanding that it is detached from the mutable original.

Sign up to request clarification or add additional context in comments.

9 Comments

Regarding the second para, that would require a double copy instead of a single copy, right?
In theory the mutable "live view" could work in much the same way that subList works for a list. Mutating the sublist writes through to the original list, and mutating a "SubStringBuilder" could mutate the backing StringBuilder.
@TedHopp That's certainly true, although that would require an introduction of a common interface, say, MutableString, and implementing it in the original StringBuilder and the (presumably hidden) SubStringBuilder -- the same way it's done in subList.
@TedHopp this would actually be a nightmare. Consider what happens if you append something to the "sub" StringBuilder. Should that be reflected in the original, too?
Having a locking strategy to make this live view possible would defeat the StringBuilders purpose of non-sync operations in order to be fast. So maybe it would be better to create another object for this entire purpose and leave StringBuilder alone. Anyway, that's an interesting discussion. :)
|
1

To go from one StringBuilder to another containing a segment of the original, you could use:

StringBuilder original = ...;
StringBuilder sub = new StringBuilder().append(original, offset, length);

This could have been provided as a method of original, but as things stand it isn't.

This aside, you should profile your code before engaging in micro-optimisations of this sort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.