473,394 Members | 2,052 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,394 software developers and data experts.

Best way to clear contents of a stringbuilder object

Using c# 3.5, what's the best way to remove the contents of a stringbuilder
object? I was using this code:

sb.Remove(1, sb.Length - 1);

but when it had 9 carriage returns in it like this:
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

I got the error:
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length at System.Text.StringBuilder.Remove(Int32 startIndex,
Int32 length)

You would think there would be a simple Clear() method.

Thanks.

--
mo*******@newsgroup.nospam
Feb 7 '08 #1
4 2218
On Thu, 7 Feb 2008 11:40:43 -0600, "moondaddy"
<mo*******@newsgroup.nospamwrote:
>Using c# 3.5, what's the best way to remove the contents of a stringbuilder
object? I was using this code:

sb.Remove(1, sb.Length - 1);

but when it had 9 carriage returns in it like this:
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

I got the error:
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length at System.Text.StringBuilder.Remove(Int32 startIndex,
Int32 length)

You would think there would be a simple Clear() method.

Thanks.
If you want to remove everything, set Length = 0.

Your call to Remove should have left the first character, but would
give an error if there were no characters in the stringbuilder.
Feb 7 '08 #2
Jon,

I am more and more happy when I see this kind of messages from you.
>
The important thing is that creating a new StringBuilder for each
iteration is simpler to understand. Unless this turns into a
performance bottleneck, you should use the simplest reasonable code and
not worry about micro-optimising. Clearing and reusing a StringBuilder
definitely counts as micro-optimisation - and it may well go wrong, as
the StringBuilder will end up in gen1 or gen2, rather than being
garbage collected quickly.
Absolutely my idea too.

Have a look at my reply too.

:-)

Cor
Feb 8 '08 #3
Moondaddy,

The simplest method is in my idea to let it go out of scope.

Cor
Feb 8 '08 #4
Thanks for the advice!
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:FE**********************************@microsof t.com...
Moondaddy,

The simplest method is in my idea to let it go out of scope.

Cor

Feb 8 '08 #5

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

Similar topics

5
by: Rich | last post by:
I have to read data from an external source, massage the data, concatenate it to one long string, then write it to a textfile. So I am experimenting with the StringBuilder object. I append the...
10
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
2
by: Novice | last post by:
Hi all, as you can see from the subject, I'm try to use an asp:button to create a new browser window and output contents to new window But default if I do the Response.Write("..." The output...
2
by: Leon | last post by:
What's the best way to send html formatted email (newsletter style)? In Code or In Web Config files? I'm thinking in code using StringWriter and HtmlTextWriter?
6
by: RonL | last post by:
What is the recommended best technique for handling errors/exceptions in ASP.Net. I've read about the following techniques: 1. Try/Catch 2. Page_Error 3. Application_Error in the...
4
by: Kuldeep | last post by:
Hi All, I am trying to read the contents of a page through its URL. My code snippet is as follows: public void mtdGetPageDataHWR() { HttpWebRequest objRequ =...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
8
by: Laser Lu | last post by:
Sometimes, I need to do some time-consuming operations based on whether a specific keyword was contained in a lengthy string. Then, for a better performance, I wrapped that lengthy string into a...
12
by: christery | last post by:
read something on yoda by Jo I think... annoying ... not like C I think... thougth I got it but no... what am I missing... public struct S { public int i;} public class C { public int i;}
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.