stringbuilder

Is String.Format as efficient as StringBuilder

Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new StringBuilder(); string cat = "cat"; sb.Append("the ").Append(cat).(" in the hat"); string s = sb.ToString(); would that be as efficient or any more efficient as having: string cat = "cat"; string s = String.Format("The {0} in the hat", cat); If so, why? E...

What is StringBuilder's RAM consumption like?

We have a few operations where we are doing a large number of large string concatenations, and have recently encountered an out of memory exception. Unfortunately, debugging the code is not an option, as this is occurring at a customer site. So, before looking into a overhaul of our code, I would like to ask: what is the RAM consumpt...

How do you inherit StringBuilder in vb.net?

I want to add my own member to the StringBuilder class, but when I go to create it IntelliSense doesn't bring it up. public class myStringBuilder() Inherits System.Text.[StringBuilder should be here] .... end class Is it even possible? thanks ...

StringBuilder: how to get the final String?

Someone told me that it's faster to concatenate strings with StringBuilder. I have changed my code but I do not see any Properties or Methods to get the final build string. How can I get the string? ...

Java performance of StringBuilder in a loop

I've a performance related question regarding use of StringBuilder. In a very long loop I'm manipulating a StringBuilder and passing it to another method like this: for (loop condition) { StringBuilder sb = new StringBuilder(); sb.append("some string"); . . . sb.append(anotherString); . . . passToMethod(sb.toStri...

Is using StringBuilder Remove method more memory efficient than creating a new StringBuilder in loop?

In C# which is more memory efficient: Option #1 or Option #2? public void TestStringBuilder() { //potentially a collection with several hundred items: string[] outputStrings = new string[] { "test1", "test2", "test3" }; //Option #1 StringBuilder formattedOutput = new StringBuilder(); foreach (string outputString in ...

How to rewrite this block of code using a StringBuilder in Java?

Given a word, I've to replace some specific alphabets with some specific letters such as 1 for a, 5 for b etc. I'm using regex for this. I understand that StringBuilder is the best way to deal with this problem as I'm doing a lot of string manipulations. Here is what I'm doing: String word = "foobooandfoo"; String converted = ""; conver...

Is StringBuilder.Replace() more efficient than String.Replace?

If you have to use String.Replace() to replace test 50 times, you essentially have to create a new string 50 times. Does StringBuilder.Replace() do this more efficiently? E.g., should I use a StringBuilder if I'm going to be replacing a lot of text, even while I won't be appending any data to it? I'm using .NET, but I assume this woul...

Creating safe SQL statements as strings

I'm using C# and .NET 3.5. I need to generate and store some T-SQL insert statements which will be executed later on a remote server. For example, I have an array of Employees: new Employee[] { new Employee { ID = 5, Name = "Frank Grimes" }, new Employee { ID = 6, Name = "Tim O'Reilly" } } and I need to end up with an array of ...

C# StringBuilder - how to escape this string:

<document.write("<SCR"+"IPT TYPE='text/javascript' SRC='"+"http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+gDomain+"/"+gDcsId+"/wtid.js"+"'><\/SCR"+"IPT>"); I need to escape the string above in order to add the whole thing to a StringBuilder but so far I must be missing something because string termination is not cor...

When do you use StringBuilder.AppendLine/string.Format vs. StringBuilder.AppendFormat?

A recent question came up about using String.Format(). Part of my answer included a suggestion to use StringBuilder.AppendLine(string.Format(...)). Jon Skeet suggested this was a bad example and proposed using a combination of AppendLine and AppendFormat. It occurred to me I've never really settled myself into a "preferred" approach fo...

StringBuilder extension method for appending a collection in C#

In C#, I'm trying to build an extension method for StringBuilder called AppendCollection() that would let me do this: var sb1 = new StringBuilder(); var sb2 = new StringBuilder(); var people = new List<Person>() { ...init people here... }; var orders = new List<Orders>() { ...init orders here... }; sb1.AppendCollection(people, p => p.T...

StringBuilder for string concatenation throws OutOfMemoryException.

We mostly tend to following the above best practice. Have a look at String vs StringBuilder But StringBuilder could throw OutOfMemoryException even when there is sufficient memory available. It throws OOM exception because it needs "continuous block of memory". Some links for reference StringBuilder OutOfMemoryException and there ar...

Howto bind TextBox control to a StringBuilder instance?

I would like several textboxes to react to changes of an underlying string. So if I were to change the content of the string, all those textboxes would change their content too. Now, I can't use the String type for that as it is immutable. So I went with StringBuilder. But the Text property of a TextBox object only takes String. Is the...

Is it worth using StringBuilder in web apps?

In web app I am splitting strings and assigning to link names or to collections of strings. Is there a significant performance benefit to using stringbuilder for a web application? EDIT: 2 functions: splitting up a link into 5-10 strings. THen repackaging into another string. Also I append one string at a time to a link everytime the l...

Using HtmlTextWriter to Render Server Controls?

I'm writing the RenderContents() method of my ASP.NET server control. The method uses an HtmlTextWriter object to render the output content. For the control I'm writing, using the HtmlTextWriter's methods seems like it will require a lot of lines of code to open and close every tag and add every attribute to the stream. In the end I feel...

Why isn't the StringBuilder class inherited from Stream?

I'm just curious about this. It strikes me that the behavior of a StringBuilder is functionally (if not technically) the same as a Stream -- it's a bin of data to which other data can be added. Again, just curious. ...

When to use StringBuilder?

I just revisited some of the books that I used to pick up VB.NET. I am not sure I've got this in my head, understand how/what StringBuilder is. What is the guidance for using? Is it best to use it if you are are concatenating 2 strings or 50? Or when the the total string length is greater than 128 characters? Or will you see a perfor...

At what point does using a StringBuilder become insignificant or an overhead?

Recently I have found myself using StringBuilder for all string concatenations, big and small, however in a recent performance test I swapped out a colleague's stringOut = string1 + "." string2 style concatenation (being used in a 10000x + loop with the StringBuilder being newed each time) for a StringBuilder just to see what difference ...

Returning Stringbuilder contents

Trivial I know, but just interested I've got a stringbuilder variable, that I want to return the contents of, but if it's empty I want to return "|", so is it best to use stringbuilder.tostring in the compare statement e.g If lReturnStringBuilder.ToString = String.Empty Then lReturnStringBuilder.Append("|") ...