Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

StringBuilder AppendFormat()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill - 14 Jul 2006 21:49 GMT
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
Barry Kelly - 14 Jul 2006 22:09 GMT
> public string BugOrFeature()
> {
[quoted text clipped - 14 lines]
>
> 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

Signature

http://barrkel.blogspot.com/

Bill - 14 Jul 2006 23:16 GMT
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.

> > public string BugOrFeature()
> > {
[quoted text clipped - 52 lines]
>
> -- Barry
Carl Daniel [VC++ MVP] - 15 Jul 2006 00:02 GMT
> Thanks Barry,
>
[quoted text clipped - 7 lines]
> 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
Marcin Hoppe - 17 Jul 2006 10:07 GMT
>>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.
>
> 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."

Signature

Marcin Hoppe


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.