Gets or sets the maximum number of characters that can be contained : StringBuilder « Development Class « C# / C Sharp






Gets or sets the maximum number of characters that can be contained

 
using System;
using System.Text;

class Sample 
{
    public static void Main() 
    {
        StringBuilder sb1 = new StringBuilder("abc");
        StringBuilder sb2 = new StringBuilder("abc", 16);
    
        Console.WriteLine("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
        Console.WriteLine("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
        Console.WriteLine("a3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"", sb1.ToString(),sb2.ToString());
        Console.WriteLine("a4) sb1 equals sb2: {0}", sb1.Equals(sb2));
    
        Console.WriteLine("Ensure sb1 has a capacity of at least 50 characters.");
        sb1.EnsureCapacity(50);
    
        Console.WriteLine("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
        Console.WriteLine("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
        Console.WriteLine("b3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"", sb1.ToString(),sb2.ToString());
        Console.WriteLine("b4) sb1 equals sb2: {0}", sb1.Equals(sb2));
    }
}

   
  








Related examples in the same category

1.Create a StringBuilder which hold 100 characters.
2.Length and Indexer
3.Use StringBuilder to reverse a string
4.using the StringBuilder methods Replace, Insert, Append, AppendFormat, and Remove:
5.String ConcatenationString Concatenation
6.illustrates reading and writing string dataillustrates reading and writing string data
7.Replace char in a StringBuilder object
8.Replace Char with String
9.Represents a mutable string of characters.
10.Create StringBuilder class using the specified capacity.
11.Create StringBuilder class that starts with a specified capacity and can grow to a specified maximum.
12.Create a StringBuilder class using the specified string.
13.Create StringBuilder class using the specified string and capacity.
14.Create StringBuilder class from the specified substring and capacity.
15.Appends various data to StringBuilder
16.Appends a composite format string
17.Appends the default line terminator to the end of the current StringBuilder object.
18.Removes all characters from the current StringBuilder instance.
19.Copies characters to a destination Char array.
20.Inserts various data types
21.Removes the specified range of characters from this instance.
22.Replaces all occurrences of a specified character in this instance with another specified character.