473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StringBuilder.A ppend() 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 2293
Why not just do a sb.Length and find out yourself?

"todorov-fkt" <to********@dis cussions.micros oft.com> wrote in message
news:2F******** *************** ***********@mic rosoft.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 ArgumentOutOfRa ngeException, as you are attempting to append
more characters then there are in the string.

Hope this helps
Jay

"todorov-fkt" <to********@dis cussions.micros oft.com> wrote in message
news:2F******** *************** ***********@mic rosoft.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".PadRigh t(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********@dis cussions.micros oft.com> wrote in message
news:2F******** *************** ***********@mic rosoft.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
2277
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 StringBuilder object and should paste into Excel via Clipboard.SetDataObject. When it comes time to paste the StringBuilder object is empty. If I strip all database code out and place the code in another project it works fine. Any ideas? Thanks for...
20
1899
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 460.6624 320.4608 350.504 220.3168
14
15048
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 a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my...
7
2104
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 example an SQL query or some XML)? This is an example of some of my current code : strReturnedXML = "<?xml version=\"1.0\"?>"; strReturnedXML += "<vcn-rsp message-id=\"" + Convert.ToString(bytMessageID) + "\">";
0
1488
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 based on 2 of the textboxes one for the start date of the events required and one for the last date of the events required. The 3rd textbox works fine. It passes the text from the textbox to the stringbuilder I built on the codebehind just fine....
15
1603
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) sb.Append("Line 3" & vbCrLf)
33
4690
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
12
2716
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 comparative analysis between the two and, surprisingly, string seems to out perform StringBuilder by a significant amount. A string concatenation takes not quite twice as long using StringBuilder than it does with a string. This doesn't sound right to...
8
2917
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 doesn't get released (or actually it looks like that) and thus the used memory keeps growing. Is there a solution to this? Regards, Arjan.
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.