Re: StringBuilder.Append() pequliarities

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Bob Grommes (bob_at_bobgrommes.com)
Date: 09/30/04


Date: Wed, 29 Sep 2004 19:44:26 -0700

What are you actually attempting to accomplish? Do you want it padded to 30
characters? If so, you have to pad the string first:

string temp = "Short string";
StringBuilder sb = new StringBuilder();
sb.Append(temp.PadRight(30,' '));

As an often-overlooked fine point, StringBuilder has a default capacity of
16 characters and will have to reallocate memory right off to take a string
longer than that; if I remember correctly the practice is to double the
capacity for each reallocation, so the above code would expand the capacity
to 32. So ... it would be more efficient to use the constructor overload to
set the capacity to a larger size in the first place. In fact all the above
could be reduced to:

StringBuilder sb = new StringBuilder("Short string".PadRight(30,' '),30);

But you're likely going to append more stuff later and would want to
allocate an even larger buffer -- perhaps one that is on the high side of
the average expected size, or even the maximum expected size.

--Bob

"todorov-fkt" <todorovfkt@discussions.microsoft.com> wrote in message
news:2F661112-C242-4F30-9E14-5C0E1D8D336F@microsoft.com...
> Hello
>
> Provided the following code:
> string temp = "Short string"; // 12 chars
> StringBuilder sb = new StringBuilder();
> sb.Append(temp, 0, 30);
>
> How many characters is the string in sb? 12 or is it filled with blanks to
> reach 30 chars?
>
> Greetings,
> Todorov



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)
  • Re: Byte Array to String
    ... retrieved text will mismatch the original characters. ... encoding the characters. ... Dim strFileData as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: A note on personal corruption as a result of using C
    ... impossible to write effective string validation routines by definition ... (Note that a string literal may contain embedded null characters; ... without resorting to abusive language. ... In practice, programmers typically use "struct" ...
    (comp.programming)