views:

282

answers:

2

I understand that I can call ToString().IndexOf(...), but I don't want to create an extra string. I understand that I can write search routine manually. I just want to know, why does not such routine already exist in the framework.

+1  A: 

Unfortunately, many of the methods implemented for String could have been implemented for StringBuilder but that was not done. Consider using extension methods to add what you care about.

binarycoder
+1  A: 

Calling ToString() on a StringBuilder doesn't create an extra object, confusingly. Internally, StringBuilder stores a String object, for performance; calling ToString() simply returns that object.

Michael Petrotta
this is not correct in the context of the question if the stringbuilder is asked to modify itself Tthen a *new* string is created, the mutability of the internal buffer is not exposed to managed code.
ShuggyCoUk
@ ShuggyCoUk: my comment was a little glib. I've removed it.
Michael Petrotta
To clarify the previous comment, there is not much overhead from calling ToString. But after you call it, the next modification to the StringBuilder will incur copying overhead. (This is a valid optimzation because ToString is usually the last thing done to a StringBuilder.) As a result of this, efficient implementations of String-like methods can't use ToString, which precludes a trivial solution to the original poster's issue.
binarycoder