StringBuilder AppendFormat()

G

Guest

Ok, so we have the following code and it produces the result seen immediately
following the function.

public string BugOrFeature()
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();

char[] cTxtSeg = new char[48];

sb1.Append("0123456789abcdefghij");
sb1.CopyTo(0, cTxtSeg, 0, 10);

sb2.Append(cTxtSeg);
sb3.AppendFormat("{0} wert wert", sb2.ToString());

return sb.ToString();
}

0123456789

Howcome the text following the format specifier in the AppendFormat() call
doesn't appear in the output string? I suspect that it has something to do
with the fact that the unused chars in the char array are initialized to '\0'
but it seems like StringBuilder ought to be smart enough to deal with that.
Is this correct behavior for the AppentFormat() member?

One workaround would be to use string.Substring() but we are led to believe
that StringBuilder (with a properly set capacity) should be more efficient
for these kinds of parsing operations.

Bill
 
B

Barry Kelly

Bill said:
public string BugOrFeature()
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();

char[] cTxtSeg = new char[48];

sb1.Append("0123456789abcdefghij");
sb1.CopyTo(0, cTxtSeg, 0, 10);

sb2.Append(cTxtSeg);
sb3.AppendFormat("{0} wert wert", sb2.ToString());

return sb.ToString();
}

0123456789

You have an error in your function: sb is not a known identifier. I have
the following program:

---8<---
using System;
using System.Text;

class App
{
static void Main()
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();

char[] cTxtSeg = new char[48];

sb1.Append("0123456789abcdefghij");
sb1.CopyTo(0, cTxtSeg, 0, 10);

sb2.Append(cTxtSeg);
sb3.AppendFormat("{0} wert wert", sb2.ToString());

Console.WriteLine(sb3.ToString());
}
}
--->8---

It prints this on the console:

---8<---
0123456789 wert wert
--->8---

Can you modify this program to reproduce your problem?

-- Barry
 
G

Guest

Thanks Barry,

I've been looking in the "Text Visualizer" in the Locals window. For some
reason it dosen't display the whole string even when the "Wrap" textbox is
checked.

I see whats happening though, the entire declared length of the char array
is being copied into sb3. I had anticipated that StringBuilder would
interpret the first '\0' in the char array as the end of the useful content
of the array like it would in a string.

Barry Kelly said:
Bill said:
public string BugOrFeature()
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();

char[] cTxtSeg = new char[48];

sb1.Append("0123456789abcdefghij");
sb1.CopyTo(0, cTxtSeg, 0, 10);

sb2.Append(cTxtSeg);
sb3.AppendFormat("{0} wert wert", sb2.ToString());

return sb.ToString();
}

0123456789

You have an error in your function: sb is not a known identifier. I have
the following program:

---8<---
using System;
using System.Text;

class App
{
static void Main()
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();

char[] cTxtSeg = new char[48];

sb1.Append("0123456789abcdefghij");
sb1.CopyTo(0, cTxtSeg, 0, 10);

sb2.Append(cTxtSeg);
sb3.AppendFormat("{0} wert wert", sb2.ToString());

Console.WriteLine(sb3.ToString());
}
}
--->8---

It prints this on the console:

---8<---
0123456789 wert wert
--->8---

Can you modify this program to reproduce your problem?

-- Barry
 
C

Carl Daniel [VC++ MVP]

Bill said:
Thanks Barry,

I've been looking in the "Text Visualizer" in the Locals window. For some
reason it dosen't display the whole string even when the "Wrap" textbox is
checked.

I see whats happening though, the entire declared length of the char array
is being copied into sb3. I had anticipated that StringBuilder would
interpret the first '\0' in the char array as the end of the useful
content
of the array like it would in a string.

That's in C or C++. The CLR string types consider \0 to be just another
character - as valid as any other.

-cd
 
M

Marcin Hoppe

Carl said:
That's in C or C++. The CLR string types consider \0 to be just another
character - as valid as any other.

That's right, but strange things happen in VS.NET debugger, according to
Jon Skeet:

http://www.yoda.arachsys.com/csharp/strings.html

"Also, some versions of VS.NET will stop displaying the contents of the
string at the first null character, and evaluate its Length property
incorrectly, calculating the value itself instead of asking the managed
code. Again, it then considers the string to finish at the first null
character."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top