The StringBuilder
class is provided in the System.Text
namespace to create an object that can hold a string value that can be changed.
An object created with the StringBuilder
class operates similarly to a string. The difference is that methods of a StringBuilder can directly manipulate
the value stored. The methods and properties for a StringBuilder object are listed in table given below.
Table :The StringBuilder Methods and Properties
Methods
or Properties
Description
|
Append: Appends an
object to the end of the current StringBuilder.
AppendFormat: Inserts
objects into a string base on formatting specifiers.
Capacity: Sets or gets
the number of characters that can be held. Capacity can be increased up to
the value of MaxCapacity.
Chars: Sets or
gets the character at a given index position using indexer notation.
EnsureCapacity: Ensures that the capacity
of StringBuilder is at least as big
as a provided
Value: If the
value is passed to EnsureCapacity,
the value of the Capacity property is set to this new value. If MaxCapacity is less than the value
passed, an exception is thrown.
Equals: Determines
whether the current StringBuilder
is equal to the value passed.
Insert: Places an object into StringBuilder at a given location.
Length: Sets or gets the length of the current value
stored in StringBuilder. Length cannot
be larger than the Capacity of StringBuilder.
If the current value is shorter than Length, the value is truncated.
MaxCapacity: Gets the maximum capacity for StringBuilder.
Remove: Removes a specified number of characters,
starting at a specified location within the current StringBuilder object.
Replace: Changes all copies of a given character with
a new character.
ToString: Converts StringBuilder
to String.
|
The
StringBuilder class can be used like
other classes. The following program uses the StringBuilder object and several of the methods and properties
presented in above table. This program has a user enter first, last, and middle
names. The values are combined into a StringBuilder
object.
The
StringBuilder class is used to represent mutable strings. It starts at a
predefined size (16 characters by default) and grows dynamically as more string
data is added. It can either grow unbounded or up to a configurable maximum.
For example:
=== Program that uses Replace () and Append () ===
using System;
using System.Text;
class Program
{
static void Main()
{
StringBuilder builder = new
StringBuilder("This is an example string");
builder.Replace("an",
"the"); // Replaces 'an' with 'the'.
Console.WriteLine(builder);
builder.Append(" that is an
example.");
Console.WriteLine(builder);
}
}
=== Output of
the program ===
This
is the example string
This
is the example string that is an example
=== Program that uses AppendFormat () ===
using System;
using System.Text;
class Program
{
static int[] sb = new int[]{1,4,6};
static void Main()
{
StringBuilder b = new StringBuilder();
foreach (int i in sb)
{
b.AppendFormat("int:
{0:0.0}\n",i);
}
Console.WriteLine(b.ToString());
}
}
=== Output of
the program ===
int: 1.0
int: 4.0
int: 6.0
=== using
Remove () ===
StringBuilder st = new
StringBuilder("Football");
st.Remove(4, 4);//Remove(int StartIndex,int
length).
Console.WriteLine(st);//Foot
=== using
Insert () ===
StringBuilder st = new StringBuilder("Foot");
st.Insert(4,
"ball");
Console.WriteLine(st);//Football
=== using
Equals () ===
StringBuilder sb = new
StringBuilder("c#");
StringBuilder sb1 = new
StringBuilder("c#");
bool b= sb.Equals(sb1);
Console.WriteLine(b);//True
The
chief advantage of a StringBuilder
object over a string object is that it preallocates a default initial amount of
memory in an internal buffer in which a string value can expand and contract.
When that memory is used, however, .NET must allocate new memory for this
internal buffer. You can reduce the frequency with which this occurs by
explicitly defining the size of the new memory using either of two techniques.
The first approach is to set this value when the StringBuilder class
constructor is called. For example, the code:
StringBuilder sb = new
StringBuilder(200);
specifies that a StringBuilder object can hold 200
characters before new memory must be allocated.
The second approach is to change the
value after the StringBuilder object
has been created, using one of the following properties or methods of the StringBuilder object:
sb.EnsureCapacity(200);
No comments:
Post a Comment