Setting the Maximum Number of Characters a StringBuilder Can Contain






Setting the Maximum Number of Characters a StringBuilder Can Contain

Problem

You want to insure that the data assigned to a StringBuilder object does not exceed a certain number of characters.

Solution

Use the overloaded constructor of the StringBuilder class, which accepts a parameter for maximum capacity. The following code creates a StringBuilder object that has a maximum size of 10 characters:

	System.Text.StringBuilder sbMax = new System.Text.StringBuilder(10, 10);
	sbMax.Append("123456789");
	sbMax.Append("0");

This code creates a StringBuilder object, sbMax, which has a maximum length of 10 characters. Nine characters are appended to this string and then a tenth character is appended without a problem. However, if the next line of code is executed:

	sbMax.Append("#");

the length of sbMax goes beyond 10 characters and an ArgumentOutOfRangeException is thrown.

Discussion

The string object is immutable and, as such, has no use for a built-in method to prevent its length from going beyond a certain point. Fortunately, the StringBuilder object contains an overloaded constructor that allows the maximum size of its string to be set. The StringBuilder constructor that you are concerned with is defined as follows:

	public StringBuilder(int initialCapacity, int maxCapacity)

For most applications, the initialCapacity and maxCapacity parameters can be identical. This way gives you the best performance, overall. If these two parameters are not identical, it is critical that they can coexist. The following line of code:

	System.Text.StringBuilder sbMax = new System.Text.StringBuilder(30, 12);

will throw an ArgumentOutOfRangeException. The reason is that the initialCapacity parameter is larger than maxCapacity, causing the exception. While you may not be explicitly writing these values for your application, if you are calculating them using some type of expression, you may run into these problems.

To handle an attempt to append characters to the StringBuilder string, forcing it beyond the maximum size, wrap any code to append text to the StringBuilder object in a try-catch block:

	try
	{
	    sbMax.Append("New String");
	}
	catch(ArgumentOutOfRangeException rangeE)
	{
	    // Handle overrun here.
	}

In addition to the Append method, you should also wrap any AppendFormat, Insert, and Replace methods of the StringBuilder object in a try-catch block. Any of these methods can allow characters to be added to the StringBuilder string, potentially causing its length to exceed its maximum specified length.

See Also

See the "StringBuilder.Append Method" topic in the MSDN documentation.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows 

essay-writings
Essay Writings 6 months ago #
essays george orwell <a href="http://essays.store">essays george orwell</a> essays george orwell <a href=http://essays.store>critical essays</a>
hire-essay-writer
Hire Essay Writer 5 months, 3 weeks ago #
persuasive essays <a href="http://essays.store">essays</a> quotes for college essays <a href=http://essays.store>essays</a>
brettscies
BrettsciesGR BrettsciesGR 5 months, 2 weeks ago #
wh0cd378108 <a href=http://levitra4you.us.com/>cheap generic levitra online</a> <a href=http://celebrex4you.us.com/>celebrex 100mg</a> <a href=http://rocaltrol4you.us.com/>buy rocaltrol</a>
bennypat
Bennypat 5 months, 2 weeks ago #
wh0cd378108 <a href=http://wellbutrin4you.us.com/>wellbutrin xl</a> <a href=http://cialis4you.us.com/>cialis</a> <a href=http://moduretic4you.us.com/>moduretic</a> <a href=http://kamagra4you.us.com/>kamagra soft</a>
aaronpromo
AaronpromoST AaronpromoST 5 months, 2 weeks ago #
wh0cd955406 <a href=http://cialisonline.fun/>20mg cialis cost</a> <a href=http://tadalissx.services/>tadalis sx</a> <a href=http://albuterol.fun/>albuterol</a> <a href=http://provera.fun/>provera medicine</a> <a href=http://tadacip.services/>tadacip</a>
michaelexato
MichaelexatoUY MichaelexatoUY 5 months, 2 weeks ago #
wh0cd955406 <a href=http://buyserpina.us.com/>serpina</a> <a href=http://singulairgeneric.us.org/>generic for singulair 10mg</a> <a href=http://buybuspar.us.com/>buspirone pill</a> <a href=http://buytorsemide.us.com/>torsemide</a> <a href=http://buysildenafil.us.com/>helpful resources</a>
charleszon
CharlesZon 5 months, 2 weeks ago #
wh0cd955406 <a href=http://tretinoincream01.store/>cheap tretinoin cream 0.25</a> <a href=http://viagraprice.services/>viagra</a> <a href=http://tadalis.services/>tadalis</a> <a href=http://sildenafilgeneric.store/>sildenafil for sale</a> <a href=http://vardenafil.fun/>vardenafil</a> <a href=http://genericforcymbalta.store/>generic of cymbalta</a> <a href=http://genericlevitra.shop/>buy levitra online canada</a> <a href=http://zofran.fun/>where can you buy zofran over the counter</a> <a href=http://tadalissx.fun/>tadalis sx</a> <a href=http://elocon.fun/>elocon</a> <a href=http://amoxicillin.services/>amoxicillin brand name</a>
brettscies
BrettsciesGR BrettsciesGR 5 months, 1 week ago #
wh0cd378108 <a href=http://cialis2018.us.com/>viagra levitra cialis</a> <a href=http://lasix365.us.org/>lasix</a> <a href=http://cymbalta4you.us.com/>duloxetine cymbalta</a> <a href=http://furosemide4you.us.com/>purchase furosemide 20 mg</a> <a href=http://requip4you.us.com/>REQUIP 25 MG</a>
bennypat
Bennypat 5 months, 1 week ago #
wh0cd378108 <a href=http://phenergan4you.us.com/>phenergan 50 mg</a> <a href=http://trazodone4you.us.com/>trazodone 75 mg</a> <a href=http://ventolin365.us.org/>ventolin</a> <a href=http://sildenafil2018.us.com/>sildenafil prices</a> <a href=http://estrace4you.us.com/>ESTRACE CREAM</a>
michaelexato
MichaelexatoUY MichaelexatoUY 5 months, 1 week ago #
wh0cd955406 <a href=http://buyalli.us.com/>alli pill</a> <a href=http://buysildenafil.us.com/>Sildenafil</a> <a href=http://alli.us.org/>alli pills</a> <a href=http://buyalli.us.org/>found it for you</a> <a href=http://tetracycline2017.us.org/>tetracycline hydrochloride</a> <a href=http://buyzithromax.us.com/>Zithromax</a> <a href=http://buycleocin.us.org/>cleocin for strep throat</a> <a href=http://buymotrin.us.org/>motrin</a>
charleszon
CharlesZon 5 months, 1 week ago #
wh0cd955406 <a href=http://levitrageneric.store/>price of levitra 20 mg</a> <a href=http://buyviagra.services/>viagra</a> <a href=http://albuterol.fun/>proair albuterol sulfate where to buy</a> <a href=http://sildenafil.fun/>sildenafil</a> <a href=http://cytoteconline.store/>cytotec online</a> <a href=http://ampicillin500mg.shop/>ampicillin 500mg</a> <a href=http://costofviagra.fun/>viagra</a> <a href=http://augmentinantibiotic.shop/>augmentin antibiotics</a> <a href=http://metforminhcl.shop/>metformin hcl 500 mg without prescription</a>
brettscies
BrettsciesGR BrettsciesGR 5 months, 1 week ago #
wh0cd378108 <a href=http://methotrexate4you.us.com/>methotrexate price</a> <a href=http://lexapro911.us.com/>Lexapro</a> <a href=http://amoxicillin4you.us.com/>amoxicillin</a>
bennypat
Bennypat 5 months ago #
wh0cd378108 <a href=http://diclofenac4you.us.com/>diclofenac</a> <a href=http://cialis365.us.org/>compare prices cialis</a> <a href=http://antabuse4you.us.com/>buy antabuse online without prescription</a> <a href=http://cialis911.us.com/>generic cialis capsules</a> <a href=http://amoxil4you.us.com/>amoxil</a>
michaelexato
MichaelexatoUY MichaelexatoUY 5 months ago #
wh0cd955406 <a href=http://buymotrin.us.org/>motrin</a> <a href=http://buyserpina.us.org/>serpina no prescription</a> <a href=http://buytadalis.us.com/>tadalis</a> <a href=http://buycleocingel.us.com/>cleocin gel</a> <a href=http://buytadalissx.us.org/>tadalis</a>
brettscies
BrettsciesGR BrettsciesGR 5 months ago #
wh0cd378108 <a href=http://medrol4you.us.com/>Medrol</a> <a href=http://clindamycin4you.us.com/>click</a>
charleszon
CharlesZon 5 months ago #
wh0cd955406 <a href=http://medrol.services/>medrol</a> <a href=http://flagylonline.store/>flagyl</a> <a href=http://genericnexium.store/>nexium in canada</a> <a href=http://buyclomidonline.shop/>purchase clomid online</a> <a href=http://retina.fun/>buy tretinoin cream</a> <a href=http://paxil.fun/>paxil</a> <a href=http://robaxin.fun/>learn more here</a> <a href=http://augmentingeneric.shop/>augmentin medicine</a>
michaelexato
MichaelexatoUY MichaelexatoUY 4 months, 4 weeks ago #
wh0cd955406 <a href=http://buyserpina.us.com/>SERPINA</a> <a href=http://buypaxil.us.com/>paxil</a> <a href=http://buytorsemide.us.org/>Torsemide 10 Mg</a> <a href=http://cleocingel.us.com/>cleocin vaginal cream</a> <a href=http://buysildenafil.us.com/>sildenafil 50 mg</a> <a href=http://buycelexa.us.com/>celexa lexapro</a> <a href=http://buyvpxl.us.org/>VPXL ONLINE</a> <a href=http://buyserpina.us.org/>serpina no prescription</a> <a href=http://singulairgeneric.us.org/>singulair</a> <a href=http://buybuspar.us.com/>generic for buspar</a> <a href=http://buyclomid.us.com/>clomid for women</a> <a href=http://buymotrin.us.org/>homepage</a>
charleszon
CharlesZon 4 months, 3 weeks ago #
wh0cd955406 <a href=http://tamoxifenonline.store/>cost of tamoxifen</a> <a href=http://genericlevaquin.store/>levaquin</a> <a href=http://metforminhydrochloride.store/>order metformin medicine</a> <a href=http://genericforlipitor.store/>lipitor</a> <a href=http://albuterolipratropium.shop/>albuterol inhaler</a> <a href=http://abilifycost.store/>info</a> <a href=http://lipitor.services/>lipitor</a> <a href=http://amoxil.fun/>amoxil</a>
bennypat
Bennypat 4 months, 3 weeks ago #
wh0cd378108 <a href=http://ampicillin4you.us.com/>ampicillin</a> <a href=http://hydrochlorothiazide4you.us.com/>hydrochlorothiazide</a> <a href=http://antabuse4you.us.com/>Buy Antabuse</a> <a href=http://cipro4you.us.com/>our site</a>
brettscies
BrettsciesGR BrettsciesGR 4 months, 3 weeks ago #
wh0cd378108 <a href=http://bactrim365.us.org/>bactrim</a> <a href=http://bactrim4you.us.com/>bactrim</a> <a href=http://levitra365.us.org/>levitra</a> <a href=http://medrol4you.us.com/>medrol medication</a> <a href=http://methotrexate4you.us.com/>medication methotrexate</a>