C#의 StringBuilder.ToString() 메서드는 aStringBuilder의 값을 문자열로 변환하는 데 사용됩니다.
구문
구문은 다음과 같습니다 -
public override string ToString (); public string ToString (int begnIndex, int len);
위에서 매개변수 begnIndex는 이 경우 부분 문자열의 시작 위치이고 len은 부분 문자열의 길이입니다.
예시
이제 예를 살펴보겠습니다 -
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