473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Setting StringBuilder.Length not behaving per documentation

Perhaps this has been discussed elsewhere - if so, I couldn't find it.

The StringBuilder.Length property is explicitly documented as padding
the length with spaces if .Length is set to a value greater than the
current length. I'm hoping to get this behavior. However, when I set
..Length to something greater than the current length, the padding is
done with null characters ('\0'), not spaces. Is this the actual
intended behavior (and the docs are wrong) or a bug (and the docs are
right)?

Any help in this would be appreciated. Thanks,

Donnie

Aug 21 '06 #1
7 2165
It looks like the docs changed:
http://msdn.microsoft.com/library/de...engthtopic.asp
vs
http://msdn2.microsoft.com/en-us/lib...er.length.aspx

I guess you would have to just find the number of spaces you are
"missing", and append via the string ctor that accepts a char and a
count (int).

Marc

Aug 21 '06 #2
Marc,

The workaround is, prior to calling StringBuilder.ToString(), to call
StringBuilder.Replace('\0', ' '). But the docs and the actual behavior
need to match. So I'm trying to get some "official" clarification as to
what's right and what I should expect.

Donnie
Marc Gravell wrote:
It looks like the docs changed:
http://msdn.microsoft.com/library/de...engthtopic.asp
vs
http://msdn2.microsoft.com/en-us/lib...er.length.aspx

I guess you would have to just find the number of spaces you are
"missing", and append via the string ctor that accepts a char and a
count (int).

Marc
Aug 21 '06 #3
http://msdn2.microsoft.com/en-us/lib....aspxdescribes the correct behavior for all versions of the framework, theprevious documentation was simply wrong.Willy.<do*********@gmail.comwrote in messagenews:11**********************@i3g2000cwc.go oglegroups.com...| Marc,|| The workaround is, prior to calling StringBuilder.ToString(), to call| StringBuilder.Replace('\0', ' '). But the docs and the actual behavior| need to match. So I'm trying to get some "official" clarification as to| what's right and what I should expect.|| Donnie||| Marc Gravell wrote:| It looks like the docs changed:| >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasslengthtopic.asp| vs| >http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.length.aspx| >| I guess you would have to just find the number of spaces you are| "missing", and append via the string ctor that accepts a char and a| count (int).| >| Marc|

Aug 21 '06 #4
Well, that assumes that your string doesn't include any null characters
at any other places... plus has the overhead of inspecting each
character in the builder so far, which .Append(new string(' ',
padLength)) doesn't; .ToString().PadRight(overallLength) is also an
option, but duplicates the (potentially large) string (efficiency claim
here assumes the padding is shorter than the content).

Re the docs matching, the fact that the docs have changed in line with
your observation suggests strongly that the original documentation was
wrong (or possibly that they were right, but fixing the bug would have
been a potentially breaking change, so it was preferable to change the
documentation). Either way, I believe that MSDN2 tells you what you
should expect to see, which agrees with what you are seeing. But MS
would be able to give a more definitive answer.

Marc

Aug 21 '06 #5
Willy,

Thanks for pointing me at better docs. I was looking at

http://msdn.microsoft.com/library/de...engthtopic.asp

(though I started my search for the Length property at msdn2. ...).

The question now is, how the heck are we supposed to reconcile
differing docs which are both live on MSDN and both, on the surface,
up-to-date and "official", accurate docs? That's just unacceptable.

Thanks again,

Donnie
Willy Denoyette [MVP] wrote:
http://msdn2.microsoft.com/en-us/lib....aspxdescribes the correct behavior for all versions of the framework, theprevious documentation was simply wrong.Willy.<do*********@gmail.comwrote in messagenews:11**********************@i3g2000cwc.go oglegroups.com...| Marc,|| The workaround is, prior to calling StringBuilder.ToString(), to call| StringBuilder.Replace('\0', ' '). But the docs and the actual behavior| need to match. So I'm trying to get some "official" clarification as to| what's right and what I should expect.|| Donnie||| Marc Gravell wrote:| It looks like the docs changed:| >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasslengthtopic.asp| vs| >http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.length.aspx| >| I guess you would have to just find the number of spaces you are| "missing", and append via the string ctor that accepts a char and a| count (int).| >| Marc|
Aug 21 '06 #6
Marc,

Obviously you're right. But using .Length has the advantage of giving
me other behavior I want (truncation) and keeping me from having to
calculate the actual number of spaces needed. I don't have any null
characters, my strings all end up at less than 300 characters, and I
only have to do that .Replace call once - right when I need to call
StringBuilder.ToString(). The simplicity seems like an acceptable
tradeoff.

Thanks for the feedback,

Donnie

Marc Gravell wrote:
Well, that assumes that your string doesn't include any null characters
at any other places... plus has the overhead of inspecting each
character in the builder so far, which .Append(new string(' ',
padLength)) doesn't; .ToString().PadRight(overallLength) is also an
option, but duplicates the (potentially large) string (efficiency claim
here assumes the padding is shorter than the content).

Re the docs matching, the fact that the docs have changed in line with
your observation suggests strongly that the original documentation was
wrong (or possibly that they were right, but fixing the bug would have
been a potentially breaking change, so it was preferable to change the
documentation). Either way, I believe that MSDN2 tells you what you
should expect to see, which agrees with what you are seeing. But MS
would be able to give a more definitive answer.

Marc
Aug 21 '06 #7
I feel your pain, but you should accept that documentation and software
aren't without bugs. What you can do to help the community, is simply click
the "Send comments on this topic.." at the bottom of each page and hope the
contents will be updated, I experienced some good results in doing so.

Willy.
<do*********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
| Willy,
|
| Thanks for pointing me at better docs. I was looking at
|
|
http://msdn.microsoft.com/library/de...engthtopic.asp
|
| (though I started my search for the Length property at msdn2. ...).
|
| The question now is, how the heck are we supposed to reconcile
| differing docs which are both live on MSDN and both, on the surface,
| up-to-date and "official", accurate docs? That's just unacceptable.
|
| Thanks again,
|
| Donnie
|
|
| Willy Denoyette [MVP] wrote:
| >
http://msdn2.microsoft.com/en-us/lib....aspxdescribes
the correct behavior for all versions of the framework, theprevious
documentation was simply wrong.Willy.<do*********@gmail.comwrote in
messagenews:11**********************@i3g2000cwc.go oglegroups.com...| Marc,||
The workaround is, prior to calling StringBuilder.ToString(), to call|
StringBuilder.Replace('\0', ' '). But the docs and the actual behavior| need
to match. So I'm trying to get some "official" clarification as to| what's
right and what I should expect.|| Donnie||| Marc Gravell wrote:| It looks
like the docs changed:|
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasslengthtopic.asp|
vs|
http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.length.aspx|
| I guess you would have to just find the number of spaces you are| >
"missing", and append via the string ctor that accepts a char and a| count
(int).| >| Marc|
|
Aug 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tom Corcoran | last post by:
Please let me know if there is a more suitable group to post in. In query analyzer I do : alter table mytable add mycolumn varchar default 50 But when I check in Enterprise manager the...
1
by: kevininstructor | last post by:
I found code on the web which takes data from an MS-Access table (my test table has three rows by five fields) copies it to the Clipboard then into a range within MS-Excel. Data is stored in a...
20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
2
by: bob | last post by:
I am binding a datatable to a datagrid in VB.NET I then want to change the width of the first column. I am unable to get the right syntax. Here is the code I tried Dim mydataset As DataSet...
4
by: Paul | last post by:
Could anyone show me the code that allows me reset a Stringbuilder back to either null or nothing or a blank space?
15
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
5
by: =?Utf-8?B?SGFycnkgVg==?= | last post by:
I'm using a StringBuilder to build a SelectCommand string that returns a number of records. If that number is >0, I want to fill an arraylist with fields from the records for a listbox. Is it...
4
by: raylopez99 | last post by:
I see that String and StringBuilder in C# / C++ do not have an easy way to set a string to null or zero length, once it is instantiated. Apparently some variant of the .NET languages do (reading...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.