Monday, March 26, 2012

StringBuilder with line break escape.

I created a StringBuilder and append string values to it, but each string I inserted a "\n" to sparate values. When I write the StringBuilder values out to a text file. The line break with "\n" did not work. Anybody knows why?

StringBuilder sb = new StringBuilder();
sb.Append("Some Value \n");
sb.Append("Another Value \n");

// create a stream writer and write all string values to the file
StreamWriter sw = File.CreateText(path)
sw.WriteLine(sb.ToString());

Another question, what is the default Capacity of a StringBuilder object created like the code above?\n is a javascript newline but you are not using javascript...instead do something like this:


StringBuilder sb = new StringBuilder();
sb.Append("Some Value" & vbcrlf);
sb.Append("Another Value" & vbcrlf);

'or

StringBuilder sb = new StringBuilder();
sb.Append("Some Value" & vbnewline);
sb.Append("Another Value" & vbnewline);


oooh, just realized that this is c#....I know the vbnewline and vbcrlf do new lines in VB but not sure about c#...sorry

Also, to your other question...from the online notes for stringbuilder:
----------------------
Notes to Implementers: The default capacity for this implementation is 16, and the default maximum capacity is Int32.MaxValue.
----------------------

MajorCats
All .net languages support the Environment.NewLine property. The property value is a constant customized specifically for the current platform. This value is automatically appended to text when using WriteLine methods.

Try that instead.

However, couldn't you simply rewrite the entire chunk as:


StreamWriter sw = File.CreateText(path)
sw.WriteLine("Some Value");
sw.WriteLine("Another Value");
...and avoid the StringBuilder entirely (since the WriteLine method will automatically insert on a newline?

Not sure about the Capacity question. From MSDN:

Capacity does not affect the string value of the current instance. Capacity can be decreased as long as it is not less than Length.

The StringBuilder dynamically allocates more space when required and increases Capacity accordingly. For performance reasons, a StringBuilder might allocate more memory than needed. The amount of memory allocated is implementation-specific.
First of al it is better to use Environment.NewLine.
But if you realy want to use it, use \r\n ... that will work
Cool, thank you all.

Environment.NewLine works perfectly in my situation, "\r\n" works as well in C#.
In addition to daver's post:

The StringBuilder allows for specifying the capacity by using the Capacity property:
sb.Capacity = 200 allows for 200 characters. The default capacity is 16.

so don't think that you should never use StringBuilder. The StringBuilder should be used in place of concatenating strings instead of using the &= as that creates a new instance of the string each time it is used and should provide better performance with large strings.
I use StringBuilder just for the performance reason you mentioned. But, I don't believe its default capacity is 16, as it's too small. The first replier said it is the maximum value of int. This sounds reasonable to me. Are you saying that it initially allocate a buffer for 16 characters, and will auto-expand the buffer as needed? The fininal buffer size would not exceed StringBuilder's default capacity. Is this right?
The default capacity for this implementation is 16, and the default maximum capacity is Int32.MaxValue (2,147,483,647; that is, hexadecimal 0x7FFFFFFF)
Hi, adec

What do you mean by "this implementation"? When I create a StringBuilder object by:

StringBuilder sb = new StringBuilder();

What does the "16" mean to me? I should not worry about anything as long as total number of char in my final string is less than Int32.MaxValue, shouldn't I?
You do not really have to worry about it. If you are pretty sure to what capacity your SB should cater, set it to limit the memory allocated. If you don't, the SB will automatically adapt. In fact you may well set it to 16 (characters) in the first place. It will dynamically adapt.
I've never done the performance comparison,
but rather than concatenate the newline (we're avoiding concatenation - right?)

try using the stringbuilder.appendformat method

sb.AppendFormat("Some Value{0}", Environment.NewLine);
sb.Appendformat("Another Value{0}", Environment.NewLine);
As a general rule, if you need to create a lot of text, do not use Concatenation. Performance wise, the Stringbuilder is superior to Concatenation, and should be used in place of this.
try this:


StringBuilder s = new StringBuilder();
s.Append("HEllo <BR>");
s.Append("World");
Response.Write(s);

I am actually kind of curious as to why one would instantiate a StringBuilder (using line terminations), use the File.CreateText to create a StreamWriter, and then call StreamWriter.Write() passing the StringBuilder to the StreamWriter when the StreamWriter does it all?
StreamWriter sw = new StreamWriter(@."path");
sw.WriteLine("SomeValue");
sw.WriteLine("AnotherValue");
sw.Close();

Is there anything wrong with this approach?
I suppose that all depends on what you need the string for.

if you need it for multiple purposes, then creating a stringbuilder is ok as its reusable.
if you need to perform replacements then appendformat is very useful.
if you have additional concatenations to perform then stringbuilder is best.
i.e. dont do (sw.WriteLine("SomeValue" & "somother value" & "yet anothervalue");

if you're just blasting data into a disk file, then perhaps a stringbuilder represents an extra [unnecessary] step.

of course, we're all just speculating about the real world requirement behind the original posters question...

0 comments:

Post a Comment