Setting the Maximum Number of Characters a StringBuilder Can Contain

Setting the Maximum Number of Characters a StringBuilder Can Contain


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


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);

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:


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


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:

	    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 6 months ago #
essays george orwell <a href="">essays george orwell</a> essays george orwell <a href=>critical essays</a>
Hire Essay Writer 5 months, 3 weeks ago #
persuasive essays <a href="">essays</a> quotes for college essays <a href=>essays</a>
BrettsciesGR BrettsciesGR 5 months, 2 weeks ago #
wh0cd378108 <a href=>cheap generic levitra online</a> <a href=>celebrex 100mg</a> <a href=>buy rocaltrol</a>
Bennypat 5 months, 2 weeks ago #
wh0cd378108 <a href=>wellbutrin xl</a> <a href=>cialis</a> <a href=>moduretic</a> <a href=>kamagra soft</a>
AaronpromoST AaronpromoST 5 months, 2 weeks ago #
wh0cd955406 <a href=>20mg cialis cost</a> <a href=>tadalis sx</a> <a href=>albuterol</a> <a href=>provera medicine</a> <a href=>tadacip</a>
MichaelexatoUY MichaelexatoUY 5 months, 2 weeks ago #
wh0cd955406 <a href=>serpina</a> <a href=>generic for singulair 10mg</a> <a href=>buspirone pill</a> <a href=>torsemide</a> <a href=>helpful resources</a>
CharlesZon 5 months, 2 weeks ago #
wh0cd955406 <a href=>cheap tretinoin cream 0.25</a> <a href=>viagra</a> <a href=>tadalis</a> <a href=>sildenafil for sale</a> <a href=>vardenafil</a> <a href=>generic of cymbalta</a> <a href=>buy levitra online canada</a> <a href=>where can you buy zofran over the counter</a> <a href=>tadalis sx</a> <a href=>elocon</a> <a href=>amoxicillin brand name</a>
BrettsciesGR BrettsciesGR 5 months, 1 week ago #
wh0cd378108 <a href=>viagra levitra cialis</a> <a href=>lasix</a> <a href=>duloxetine cymbalta</a> <a href=>purchase furosemide 20 mg</a> <a href=>REQUIP 25 MG</a>
Bennypat 5 months, 1 week ago #
wh0cd378108 <a href=>phenergan 50 mg</a> <a href=>trazodone 75 mg</a> <a href=>ventolin</a> <a href=>sildenafil prices</a> <a href=>ESTRACE CREAM</a>
MichaelexatoUY MichaelexatoUY 5 months, 1 week ago #
wh0cd955406 <a href=>alli pill</a> <a href=>Sildenafil</a> <a href=>alli pills</a> <a href=>found it for you</a> <a href=>tetracycline hydrochloride</a> <a href=>Zithromax</a> <a href=>cleocin for strep throat</a> <a href=>motrin</a>
CharlesZon 5 months, 1 week ago #
wh0cd955406 <a href=>price of levitra 20 mg</a> <a href=>viagra</a> <a href=>proair albuterol sulfate where to buy</a> <a href=>sildenafil</a> <a href=>cytotec online</a> <a href=>ampicillin 500mg</a> <a href=>viagra</a> <a href=>augmentin antibiotics</a> <a href=>metformin hcl 500 mg without prescription</a>
BrettsciesGR BrettsciesGR 5 months, 1 week ago #
wh0cd378108 <a href=>methotrexate price</a> <a href=>Lexapro</a> <a href=>amoxicillin</a>
Bennypat 5 months ago #
wh0cd378108 <a href=>diclofenac</a> <a href=>compare prices cialis</a> <a href=>buy antabuse online without prescription</a> <a href=>generic cialis capsules</a> <a href=>amoxil</a>
MichaelexatoUY MichaelexatoUY 5 months ago #
wh0cd955406 <a href=>motrin</a> <a href=>serpina no prescription</a> <a href=>tadalis</a> <a href=>cleocin gel</a> <a href=>tadalis</a>
BrettsciesGR BrettsciesGR 5 months ago #
wh0cd378108 <a href=>Medrol</a> <a href=>click</a>
CharlesZon 5 months ago #
wh0cd955406 <a href=>medrol</a> <a href=>flagyl</a> <a href=>nexium in canada</a> <a href=>purchase clomid online</a> <a href=>buy tretinoin cream</a> <a href=>paxil</a> <a href=>learn more here</a> <a href=>augmentin medicine</a>
MichaelexatoUY MichaelexatoUY 4 months, 4 weeks ago #
wh0cd955406 <a href=>SERPINA</a> <a href=>paxil</a> <a href=>Torsemide 10 Mg</a> <a href=>cleocin vaginal cream</a> <a href=>sildenafil 50 mg</a> <a href=>celexa lexapro</a> <a href=>VPXL ONLINE</a> <a href=>serpina no prescription</a> <a href=>singulair</a> <a href=>generic for buspar</a> <a href=>clomid for women</a> <a href=>homepage</a>
CharlesZon 4 months, 3 weeks ago #
wh0cd955406 <a href=>cost of tamoxifen</a> <a href=>levaquin</a> <a href=>order metformin medicine</a> <a href=>lipitor</a> <a href=>albuterol inhaler</a> <a href=>info</a> <a href=>lipitor</a> <a href=>amoxil</a>
Bennypat 4 months, 3 weeks ago #
wh0cd378108 <a href=>ampicillin</a> <a href=>hydrochlorothiazide</a> <a href=>Buy Antabuse</a> <a href=>our site</a>
BrettsciesGR BrettsciesGR 4 months, 3 weeks ago #
wh0cd378108 <a href=>bactrim</a> <a href=>bactrim</a> <a href=>levitra</a> <a href=>medrol medication</a> <a href=>medication methotrexate</a>