What is the difference between StringWriter
and StringBuilder
and when should I use one or the other?
I don't think any of the existing answers really answer the question. The actual relationship between the two classes is an example of the adaptor pattern.
StringWriter
implements all its Write...
methods by forwarding on to an instance of StringBuilder
that it stores in a field. This is not merely an internal detail, because StringWriter
has a public method GetStringBuilder that returns the internal string builder, and also a constructor that allows you to pass in an existing StringBuilder
.
So StringWriter
is an adaptor that allows StringBuilder
to be used as a target by code that expects to work with a TextWriter
. In terms of basic behaviour there is clearly nothing to choose between them... unless you can measure the overhead of forwarding the calls, in which case StringWriter
is slightly slower, but that seems very unlikely to be significant.
So why didn't they make StringBuilder
implement TextWriter
directly? This is a grey area, because the intention behind an interface is not necessarily always clear at first glance.
TextWriter
is very nearly an interface to something that accepts a stream of characters. But it has an additional wrinkle: a property called Encoding. This implies that TextWriter
is an interface to something that accepts a stream of characters and also converts them into bytes.
This is a useless vestige in StringWriter
because it performs no encoding. The documentation says:
This property is necessary for some XML scenarios where a header must be written containing the encoding used by the StringWriter. This allows the XML code to consume an arbitrary StringWriter and generate the correct XML header.
But that can't be right, because there is no way for us to specify the value of Encoding
for StringWriter
. The property always has the value UnicodeEncoding
. Consequently any code that examined this property to build an XML header into would always say utf-16
. For example:
var stringWriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(stringWriter))
xDocument.WriteTo(xmlWriter);
That produces the header:
<?xml version="1.0" encoding="utf-16"?>
What if you used File.WriteAllText to write your XML string to a file? By default, you'd then have a utf-8
file with a utf-16
header.
In such scenarios it would be safer to use StreamWriter
, and construct it with a file path, or a FileStream
, or if you want to examine the data then use a MemoryStream
and so obtain an array of bytes. All these combinations would ensure that the encoding to bytes and the header generation were being guided by the same Encoding
value in your StreamWriter
.
The aim of the Encoding
property was to allow generators of character streams to include accurate information about the encoding into the character stream itself (such as in the XML example; other examples include email headers, and so on).
But by introducing StringWriter
, that link between content generation and encoding is broken, and so such automatic mechanisms stop working and become potentially error prone.
Nevertheless, StringWriter
is a useful adaptor if you are careful, i.e. you understand that your content generation code should not depend on the meaningless value of the Encoding
property. But that kind of caveat is commonly associated with the adaptor pattern. It's often a sort of hack to allow you to fit a square-ish-but-almost round peg into a round hole.
StringWriter
derives from TextWriter
, which allows various classes to write text without caring where it's going. In the case of StringWriter
, the output is just into memory. You would use this if you're calling an API which needs a TextWriter
but you only want to build up results in memory.
StringBuilder
is essentially a buffer which allows you to perform multiple operations (typically appends) to a "logical string" without creating a new string object each time. You would use this to construct a string in multiple operations.
Building on the previous (good) answers, StringWriter is actually much more versatile than StringBuilder, providing lots of overloads.
For example:
While Stringbuilder only accepts a string or nothing for Appendline
StringBuilder sb = new StringBuilder();
sb.AppendLine("A string");
StringWriter can take a string format directly
StringWriter sw = new StringWriter();
sw.WriteLine("A formatted string {0}", DateTime.Now);
With StringBuilder one must do this (or use string.Format, or $"")
sb.AppendFormat("A formatted string {0}", DateTime.Now);
sb.AppendLine();
Not do or die stuff, but still a difference
The StringBuilder
class is basically a mutable string, a helper class to construct an immutable string. The StringWriter
is built on top to add more convenience functions for string formatting.
A StringWriter
is used to write text to a file memory and a StringBuilder
is used to append strings together in a memory-efficient manner.
StringBuilder
and StringReader
are used to improve performance in different situations.
Use StringBuilder
to improve performance on string manipulation such as concatenation, modifying string repeatedly.
Random rnd = new Random();
StringBuilder sb = new StringBuilder();
// Generate 10 random numbers and store in sb.
for (int i = 0; i < 10; i++)
{
sb.Append(rnd.Next().ToString("N5"));
}
Console.WriteLine("The original string:");
Console.WriteLine(sb.ToString());
// Decrease each number by one.
for (int ctr = 0; ctr < sb.Length; ctr++)
{
if (Char.GetUnicodeCategory(sb[ctr]) == System.Globalization.UnicodeCategory.DecimalDigitNumber)
{
int number = (int)Char.GetNumericValue(sb[ctr]);
number--;
if (number < 0)
number = 9;
sb[ctr] = number.ToString()[0];
}
}
Console.WriteLine("\nThe new string:");
Console.WriteLine(sb.ToString());
Use StringReader
to parse a large amount of text in separate lines and minimize memory use while processing data. See next example where ReadLine method on StringReader simply scans for the next newline starting at the current postion, and then return a sbstring based on the field string.
using (StringReader sr = new StringReader("input.txt"))
{
// Loop over the lines in the string or txt file.
int count = 0;
string line;
while((line = sr.ReadLine()) != null)
{
count++;
Console.WriteLine("Line {0}: {1}", count, line);
}
}
StringBuilder
is used to mass string concatenation. It's more effective for more then 5 string, As far as i know, then String.Concat()
. Also it can be used with specific format (.AppendFormat()
)
来源:https://stackoverflow.com/questions/602279/stringwriter-or-stringbuilder