Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Thursday, April 12, 2012

Strings and StringBuilder in C#

Here in this article we will discuss about Strings and StringBuilder in C#.Net and what is the difference between String and StringBuilder in C#.Net. In the last article we have discussed about Delegates in

- In C#, a string is a built in and primitive data type. It maps to the System.String class. The objects of the string class or the string is are immutable by nature.

- Then the question is now what is the exact meaning of immutable by nature means, the state of the object cannot be changed by any operation. If we will take one example or casestudies then we can able to understand the meaning of immutable exactly.
- Let's take the case that when we are calling the ToUpper() method on string , it actually doesn't change the original string but creates and returns a new string object that is the uppercase representation of the original object. So here more memory is also required. For this reason to avoid this disadvantage StringBuilder come. So we will discuss the stringBuilder later. Let's continue with strings.

- There are different properties or methods for string class like Length, CompareTo(string s), Compare(string s1,string s2), Equals(string s), Concat(string s), Copy(string s), Intern(string s), StartsWith(string s), EndsWith(string s), Replace(string,string)  etc.

The StringBuilder:
- In order to avoid the disadvantages of string, we go with the StringBuilder class. When there are more string manipulation works are there then its better to choose StringBuilder instead of String.

- The mutable version of string in .net platform is the StringBuilder class . The objects of the class are mutable. The objects of the stringBuilder class are mutable that means their state can be changed by their operations.

- For Example if you call the Append() method on a StringBuilder class object, the new string will be added to the end of the original object. This StringBuilder class is mapped from System.Text.StringBuilder class.

- The System.Text.StringBuilder class is very simillar to the System.String class with the difference that it is mutable, i.e the internal state of its object can be modified by its operations. Unlike in the string class, you must first call the constructor of a StringBuilder to instanciate its object.

string s = "This is by the string ";
StringBuilder sb = new StringBuilder("This is by the StringBuilder");

- StringBuilder is somewhat similar to the ArrayList and the other collections as the size of the string it contains grows automatically as the size of the string it contains changes.

- There are some properties just like the string are like follows: These are like Length, Capacity, Append(), Insert(), Replace(char,char), Replace(string,string), Remove(int st,int length),Equals(StringBuilder).

For example:
StringBuilder sb = new StringBuilder("Don't Forget");
string s = "Your Parents";
console.WriteLine("StringBuilder after appending '{0}' is '{1}'",S, sb.Append(s));
Now the output will be StringBuilder after appending 'Your Parents' is 'Don't Forget Your Parents'.

Also read some Asp.Net Articles. Also you can check Interview Questions here.

0 on: "Strings and StringBuilder in C#"