C# में StringBuilder.ToString() विधि का उपयोग aStringBuilder के मान को एक स्ट्रिंग में बदलने के लिए किया जाता है।
सिंटैक्स
वाक्य रचना इस प्रकार है -
public override string ToString (); public string ToString (int begnIndex, int len);
ऊपर, पैरामीटर begnIndex इस उदाहरण में सबस्ट्रिंग की शुरुआती स्थिति है, जबकि लेन सबस्ट्रिंग की लंबाई है।
उदाहरण
आइए अब एक उदाहरण देखें -
using System; using System.Text; public class Demo{ public static void Main(){ StringBuilder strBuilder = new StringBuilder("Katie"); Console.WriteLine("String = "+strBuilder.ToString()); Console.WriteLine("\nStringBuilder capacity = "+strBuilder.Capacity); Console.WriteLine("StringBuilder length = "+strBuilder.Length); strBuilder.EnsureCapacity(35); Console.WriteLine("\nStringBuilder capacity= "+strBuilder.Capacity); Console.WriteLine("StringBuilder length = "+strBuilder.Length); strBuilder.EnsureCapacity(0); strBuilder.Length = 0; Console.WriteLine("\nStringBuilder capacity= "+strBuilder.Capacity); Console.WriteLine("StringBuilder length = "+strBuilder.Length); } }
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
String = Katie StringBuilder capacity = 16 StringBuilder length = 5 StringBuilder capacity= 35 StringBuilder length = 5 StringBuilder capacity= 35 StringBuilder length = 0
उदाहरण
आइए अब एक और उदाहरण देखें -
using System; using System.Text; public class Demo{ public static void Main(){ StringBuilder strBuilder = new StringBuilder("TomHanks"); Console.WriteLine("StringBuilder = "+strBuilder.ToString()); Console.WriteLine("String = "+strBuilder.ToString(3, 5)); Console.WriteLine("\nStringBuilder capacity = "+strBuilder.Capacity); Console.WriteLine("StringBuilder length = "+strBuilder.Length); strBuilder.EnsureCapacity(35); Console.WriteLine("\nStringBuilder capacity= "+strBuilder.Capacity); Console.WriteLine("StringBuilder length = "+strBuilder.Length); strBuilder.EnsureCapacity(0); strBuilder.Length = 0; Console.WriteLine("\nStringBuilder capacity= "+strBuilder.Capacity); Console.WriteLine("StringBuilder length = "+strBuilder.Length); } }
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
StringBuilder = TomHanks String = Hanks StringBuilder capacity = 16 StringBuilder length = 8 StringBuilder capacity= 35 StringBuilder length = 8 StringBuilder capacity= 35 StringBuilder length = 0