StringBuilder and indexof

Forgive me if you've seen this before. I seem to be losing track of postings. Mechanical failure at my end i suppose:-)

Why isn't there an indexof method for stringbuilder's

I would love to be able to code

Dim sb as new stringbuilder
sb.append("How Now Brown Cow")

dim l as integer = sb.indexof ("ow")

if l>-1 then sb.remove(l, 2)

ALTERNATIVELY

I note in passing that cstr(sb) does not work.

Could someone explain precisely what happens when you say sb.tostring

I'm assuming that you get the contents of sb stuck in another location in memory.

Regards,

Al Christoph
Who really thinks stringbuilder is great besides this.



Answer this question

StringBuilder and indexof

  • Raymundo Chapa94595

    Thanks,

    stringbuilder.tostring.indexof is what i ended up doing

    What i'd like to know is does this result in lots of strings being created for the GC to cleanup or does some magic occur that let's the indexof work on the underlying memory in the stringbuilder.

    Typically when I'm doing this it's inside a loop looking for tokens and processing.

    Incidentally while we are on the subject is there any kind of token parsing short of regular expressions like a split on steroids. (As near as I can tell if you pass a string to split it uses each character individually to do the splitting rather than looking for the entire string as a divider. Correct )

    Regards,

    Al


  • John Portnov

    You should keep in mind that ToString will make a copy of the data in the string builder. If you call ToString everytime you append to a string builder you will loose all the performance benefits of string builder, and will actually have worse performing code than if you just did regular string concatination. The performance benefits of string builder come from the specialized memory allocation algorithms it uses which enable it to handle growing a string in response to an append operation very quickly. Appending regular strings in a loop using "&" is slow, which is why people generally recomend string builder over "&".

    I would reccomend that you call ToString on a string builder only once, after you've done all the appending and removing that you need on the string builder just before you want to use the final results.

    Unfortuantely, this means that you will have to implement your own IndexOf. You can use do this by doing a character by charcter comparison between the string builder and the target string.

    If you are only concatinating one or two strings, or are not in a performance sensitive scenario, then I would reccomend that you just skip the string builder all together and just use "&".

    -Scott Wisniewski
    MS VB Compiler Dev


  • SRbake1311

    sb.ToString() returns the full string stored in the string builder.

    StringBuilders are the way to go, and best practice, for handling strings - it also uses the memory as needed than say declaring a string type and storing just 1 character in it - makes ineffecient use of memory.

    you can use indexOf by doing a ToString().IndexOf()

    if you need to remove a portion of string in the StringBuilder, use the "Remove" method

    if you need to insert a string at a specified position, use the "Insert" method.

    hope it helps!



  • K007

    When you call ToString on a string builder it creates a copy of the contents of the string builder

    If you are calling it on a string builder in a loop you will get many strings.

    In VS 2005 (.NET Framework 2.0) there is an override of Split that takes in an array string delimeters. Check it out here: http://msdn2.microsoft.com/en-us/library/1bwe3zdy.aspx.

    -Scott Wisniewski


  • StringBuilder and indexof