|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
StringBuilder AppendFormat()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 Bill <B***@discussions.microsoft.com> wrote:
Show quote > public string BugOrFeature() You have an error in your function: sb is not a known identifier. I have> { > 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 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 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. Show quote "Barry Kelly" wrote: > Bill <B***@discussions.microsoft.com> wrote: > > > 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 > > -- > http://barrkel.blogspot.com/ > "Bill" <B***@discussions.microsoft.com> wrote in message That's in C or C++. The CLR string types consider \0 to be just another news:67D5297F-765E-40F7-B32A-B8323D0678C2@microsoft.com... > 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. character - as valid as any other. -cd Carl Daniel [VC++ MVP] wrote:
>>I've been looking in the "Text Visualizer" in the Locals window. For some That's right, but strange things happen in VS.NET debugger, according to >>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. 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." -- Marcin Hoppe |
|||||||||||||||||||||||