stringbuilder

String.Join vs. StringBuilder: which is faster?

In a previous question about formatting a double[][] to CSV format, Marc Gravell said that using StringBuilder would be faster than String.Join. Is this true? ...

P/Invoke with [Out] StringBuilder / LPTSTR and multibyte chars: Garbled text?

I'm trying to use P/Invoke to fetch a string (among other things) from an unmanaged DLL, but the string comes out garbled, no matter what I try. I'm not a native Windows coder, so I'm unsure about the character encoding bits. The DLL is set to use "Multi-Byte Character Set", which I can't change (because that would break other project...

Java StringBuilder and Thread Safety

I am building up a String out of multiple pieces and want to use either StringBuffer or StringBuilder to do so. From the Java 5 docs, I see that StringBuilder is preferred when possible, with the caveat that "Instances of StringBuilder are not safe for use by multiple threads". From this statement I understand that I should not have a si...

StringBuilder.Append Vs StringBuilder.AppendFormat

I was wondering about StringBuilder and I've got a question that I was hoping the community would be able to explain. Let's just forget about code readability, which of these is faster and why? StringBuilder.Append: StringBuilder sb = new StringBuilder(); sb.Append(string1); sb.Append("----"); sb.Append(string2); StringBuilder.Appen...

C# or Java: Prepend strings with StringBuilder?

I know we can append strings using StringBuilder. Is there a way we can prepend strings (ie: Add strings infront of a string) using StringBuilder so we can keep the performance benefits that StringBuilder offers? ...

String Concatenation unsafe in C#, need to use StringBuilder?

My question is this: Is string concatenation in C# safe? If string concatenation leads to unexpected errors, and replacing that string concatenation by using StringBuilder causes those errors to disappear, what might that indicate? Background: I am developing a small command line C# application. It takes command line arguments, performs...

String concatenation in C# with interned strings

I know this question has been done but I have a slightly different twist to it. Several have pointed out that this is premature optimization, which is entirely true if I were asking for practicality's sake and practicality's sake only. My problem is rooted in a practical problem but I'm still curious nonetheless. I'm creating a bunc...

best way of replacing all tags in a string with java

I have a service method that takes a String and then replaces tags in the String with items from a tag library. As follows: for( MetaDataDTO tag : tagValues ) { message = message.replace( tag.getKey(), tag.getText1() ); } Obviously; this make heaps of new strings and is BAD. But the StringBuilder replace method is cumbersome to ...

Inverse String.Replace - Faster way of doing it?

I have a method to replace every character except those I specify. For example, ReplaceNot("test. stop; or, not", ".;/\\".ToCharArray(), '*'); would return "****.*****;***,****". Now, this is not an instance of premature optimization. I call this method quite a few times during a network operation. I found that on longer strings...

String or StringBuilder return values?

If I am building a string using a StringBuilder object in a method, would it make sense to: Return the StringBuilder object, and let the calling code call ToString()? return sb; OR Return the string by calling ToString() myself. return sb.ToString(); I guess it make a difference if we're returning small, or large strings. What wo...

go beyond Integer.MAX_VALUE constraints in Java

Setting aside the heap's capacity, are there ways to go beyond Integer.MAX_VALUE constraints in Java? Examples are: Collections limit themselves to Integer.MAX_VALUE. StringBuilder / StringBuffer limit themselves to Integer.MAX_VALUE. ...

Dynamically generating RDLC - What stops me from using a StringBuilder for building the XML

Hi I'm building a Local Report. Because of some limitations of the Hidden property, I need to dynamically generate the report. I found some documentation here. The ReportViewer Control needs a stream. I don't really like the method that's used in the documentation. And building an XmlDocument isn't very readable imo. Is there some...

StringBuilder and capacity?

I have created test application to test, Is StringBuilder copy data to another instance and grow it buffer when its length is more than current capacity and verify in ildasm.exe but it seem identical. How to verify StringBuilder will copy its data into new instance and grow the buffer by the specified limit? ...

String.Format vs "string" + "string" or StringBuilder?

Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city, state); or city + ", " + state; or StringBuilder sb = new StringBuilder(); sb.Append(...

Problem with StringBuilder and XML Literals

I'm having a problem using XML literals with a StringBuilder in VB 2008. If I use this code everything is fine. Dim html As New System.Text.StringBuilder html.Append(<html><body></body></html>) MsgBox("hello") Now the problem is I want to wrap HTML around something that is generated in code. html.Append(<html><body>) msgbox("nothi...

How can I change this StringBuilder-to-XML code to LINQ-to-XML?

In my application I build an XML file with this code using StringBuilder: StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine); sb.Append(String.Format("<{0}>{1}", _pluralCamelNotation, Environment.NewLine)); for (int index = 0; index < 3; index++) { sb.Append(String....

stringbuilder append override my record if (!IsPostback).

How to avoid overriding? public partial class test : System.Web.UI.Page { StringBuilder sb = new StringBuilder(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { sb.Append("one"); lbl.Text = sb.ToString(); } } protected void cmdSb_Click(obj...

ASP.NET HTML control with clean output?

I am developing an ASP.NET web application at work, and I keep running into the same issue: Sometimes I want to write HTML to the page from the code-behind. For example, I have a page, Editor.aspx, with output which varies depending on the value of a GET variable, "view." In other words, "Editor.aspx?view=apples" outputs different HTML ...

Accessing the same string(StringBuilder) using multi-thread.

Hi, My problem is if I use multi-thread on the same string sometime the string won't get replace.(I wrote this on notepad so syntax may be wrong) using System.Thread ... Others ofcourse class .... { private static StringBuild container = new StringBuilder(); static void Main(...) { container.Append(Read From File(K...

How do I port an extension in VB.NET to C#?

Hello SO: I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine method (so I would not have to use one of the arguments for a new line character): Imports System.Runtime.CompilerServices Public Module sbExtension <Extension()> _ Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _ ...