Forcing StreamWriter to change Encoding

Just wrap it in a FileStream. StreamWriter sw = new StreamWriter( new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite), Encoding.UTF8 ); If you want to append, use FileMode.Append instead. You should also call Dispose() on a try/finally block, or use a using block to dispose the object when it exceeds the using scope: using( var sw = new StreamWriter( … Read more

Don’t use StringBuilder or foreach in this hot code path

I made the code change and yes it made a huge difference in number of allocations (GetEnumerator()) calls vs not. Imagine this code is millions of times per second. The number of enumerators allocated is ridiculous and can be avoided. edit: We now invert control in order to avoid any allocations (writing to the writer … Read more

What is an efficient way to compare StringBuilder objects

As you apparently already know, StringBuilder inherits equals() from java.lang.Object, and as such StringBuilder.equals() returns true only when passed the same object as an argument. It does not compare the contents of two StringBuilders! If you look at the source, you’ll conclude that the most efficient comparison (that didn’t involve creating any new objects) would … Read more

Java CharAt() and deleteCharAt() performance

For String, StringBuffer, and StringBuilder, charAt() is a constant-time operation. For StringBuffer and StringBuilder, deleteCharAt() is a linear-time operation. StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is that the former is synchronized (so is thread-safe) while the latter is not.

Is .NET’s StringBuilder thread-safe

Absolutely not; here’s a simple example lifted from 4.0 via reflector: [SecuritySafeCritical] public StringBuilder Append(char value) { if (this.m_ChunkLength < this.m_ChunkChars.Length) { this.m_ChunkChars[this.m_ChunkLength++] = value; } else { this.Append(value, 1); } return this; } The attribute just handles callers, not thread-safety; this is absolutely not thread-safe. Update: looking at the source he references, this is … Read more

StringBuilder vs. String considering replace

It is true that StringBuilder tends to be better than concatenating or modifying Strings manually, since StringBuilder is mutable, while String is immutable and you need to create a new String for each modification. Just to note, though, the Java compiler will automatically convert an example like this: String result = someString + someOtherString + … Read more

StringBuilder Vs StringWriter/StringReader

What is the possible use of StringWriter and StringReader, which cannot be done by StringBuilder itself? StringReader and StringWriter derive from TextReader and TextWriter respectively. So what they can do act as a TextReader or TextWriter instance, which string or StringBuilder cannot because they do not derive either of those types. What is the practical … Read more

What does —-s mean in the context of StringBuilder.ToString()?

The source code for the published Reference Source is pushed through a filter that removes objectionable content from the source. Verboten words are one, Microsoft programmers use profanity in their comments. So are the names of devs, Microsoft wants to hide their identity. Such a word or name is substituted by dashes. In this case … Read more