String
Class
|
StringBuilder
Class
|
String
class belongs to the namespace System. |
StringBuilder
class belongs to the namespace System.Text. |
String
class is immutable. Immutable means that the string cannot be changed.
Consider the following example:
class sampleClass {
public static void Main() {
string sampleStr = "Hai";
sampleStr = "Hello";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello
In
this example, you have created a string called sampleStr. You have
initially assigned the value "Hai". And then you try to
overwrite its value with "Hello". You get the overwritten
value as output. But the problem lies in the number of strings that
get created in memory. When you create the string as "Hai",
this string gets created in the memory. When you try to change the
value to "Hello", instead of overwriting the existing
"Hai" string it will create a new string in the memory
and assign this new string "Hello" to sampleStr.
|
StringBuilder
class is mutable. Consider the following example:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder("Hai",10);
sampleSB = new
StringBuilder("Hello");
Console.WriteLine(sampleSB);
}
}
Output of this code will be:
Hello
In
this example, you are doing the same thing. But the string "Hai"
will be overwritten as "Hello" and no new strings will
be created in the memory.
|
You
can directly assign a string to string class instance. For example,
String sampleStr = "Hai" is valid.
|
You
cannot directly assign a string to StringBuilder instance. For example,
StringBuilder sampleSB = "Hai" will lead to the following
error:
"cannot implicitly convert type 'string' to 'System.Text.StringBuilder'
"
You can assign a string to StringBuilder using the following statement:
StringBuilder sampleSB = new
StringBuilder("Hai");
|
String
concatenation is done using + operator. Here is an example:
class sampleClass {
public static void Main() {
string sampleStr = "Hello!";
sampleStr += " Good Day!";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello! Good Day!
Here
you have used += operator to perform both concatenation and assignment
using
|
String
concatenation is done using Append method. Here is an example:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder("Hello!");
sampleSB.Append("Good
Day!");
Console.WriteLine(sampleSB);
}
}
Output of this code will be:
Hello! Good Day!
|
String
concatenation is done using + operator. Here is an example:
class sampleClass {
public static void Main() {
string sampleStr = "Hello!";
sampleStr += " Good Day!";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello! Good Day!
Here
you have used += operator to perform both concatenation and assignment
using single operator. You can also use + and = separately as shown
below:
sampleStr = sampleStr + " Good Day!";
|
|
During
string concatenation, additional memory will be allocated. |
During
string concatenation, additional memory will be allocated if and only
if the string buffer's capacity is reached. |
During
string concatenation, additional memory will be allocated if and only
if the string buffer's capacity is reached. |
If the
number of concatenations to be done is random or not known, then it
is recommended to use stringBuilder |
You
cannot set a limit (specifying how many strings can be concatenated)
to a string object using string class. |
You
can set a limit to StringBuilder using the member called capacity
which will by default have the value 16. You can override it to any
number. The maximum value acceptable is equivalent to MaxValue of
Int32. If you feel that you do not want to reserve 16 as the capacity
then you can very well redefine it. However the capacity will dynamically
grow based on the number of strings that you append.
Here
is an example demonstrating the usage of capacity:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder();
Console.WriteLine(
sampleSB.Capacity);
sampleSB.Capacity = 1;
Console.WriteLine(
sampleSB.Capacity);
sampleSB.Append("str1");
sampleSB.Append("str2");
Console.WriteLine(
sampleSB.Capacity);
}
}
Output of this code will be:
16
1
8 |