Hi........
Oues: Hello all of you now i am write a string based program but i am not understand What is used of System.Text.StringBuilder ? and please explain .....What the advantage of using System.Text.StringBuilder over System.String?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Dec 4, 2011, 5:39 AM
Satyapriya NayakPosted Dec 3, 2011, 11:38 PM
The String object is immutable which means, every time you use any of the methods in the String class, you create a new string object in memory.
The StringBuilder class can be used when you want to modify a string without creating a new object. StringBuilder class is present in System.Text namespace where String class is present in System namespace
Example for string builder class:using System.Text;private void DeleteRecords(StringCollection sc){con = new SqlConnection(connStr);StringBuilder sb = new StringBuilder(string.Empty);foreach (string item in sc){const string sqlStatement = "DELETE FROM employee WHERE EmpId";sb.AppendFormat("{0}='{1}'; ", sqlStatement, item);}try{con.Open();SqlCommand cmd = new SqlCommand(sb.ToString(), con);cmd.CommandType = CommandType.Text;cmd.ExecuteNonQuery();}catch (System.Data.SqlClient.SqlException ex){string msg = "Deletion Error:";msg += ex.Message;throw new Exception(msg);}finally{con.Close();}Advantage
StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it's being operated on, a new instance is created.Thanks