473,412 Members | 4,196 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,412 software developers and data experts.

StringBuilder.Append() pequliarities

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
Nov 16 '05 #1
3 2277
Why not just do a sb.Length and find out yourself?

"todorov-fkt" <to********@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.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

Nov 16 '05 #2
Todorov,
Have you tried it?

I receive an ArgumentOutOfRangeException, as you are attempting to append
more characters then there are in the string.

Hope this helps
Jay

"todorov-fkt" <to********@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.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

Nov 16 '05 #3
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" <to********@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.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

Nov 16 '05 #4

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

Similar topics

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...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
7
by: Mike P | last post by:
I read somewhere that if you are concatenating more than 2 or 3 strings you should use StringBuilder as it will use up less resources. Does this apply when you are building up a string (for...
0
by: I am Sam | last post by:
Ok I don't know what is the problem with my code But I am trying to build a newsletter that gathers parameters from 3 textbox controls and a Listbox control. The form then queries the event table...
15
by: Joe Fallon | last post by:
Is concatentaion inside of a Stringbuilder "evil"? Which is the preferred syntax below? Syntax #1 Dim sb As New StringBuilder sb.Append("Line 1" & vbCrLf) sb.Append("Line 2" & vbCrLf)...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.