StringBuilder로 할 수 있는 작업들
StringBuilder는 문자열의 내용을 수정하는 메소드들을 제공합니다. 이들을 다음 표로 정리하오니 참고하시기 바랍니다.
원하는 작업
|
StringBuilder가 제공하는
필드 또는 메소드
|
사용 예
|
출력 결과
|
새로운 문자열을 뒤에 추가한다. | StringBuilder Append(string value) | StringBuilder strBldr = new StringBuilder("Super"); strBldr.Append(" Star"); Console.WriteLine(strBldr.ToString()); | Super Star (해설: "Super" 뒤에 " Star"가 추가되어 "Super Star"가 됩니다.) |
중간에 문자열을 삽입한다. | StringBuilder Insert( int index, // 이 위치에 string value) // 를 삽입 | StringBuilder strBldr = new StringBuilder("Super Star"); strBldr.Insert(6, "Real "); Console.WriteLine(strBldr.ToString()); | Super Real Star (해설: 현재 문자열의 6번째 위치는 두번째 S이고, 이 위치부터 문자열 "Real "이 삽입됩니다.) |
현재 문자열의 일부를 지운다. | StringBuilder Remove( int startIndex, // 이 위치부터 int length) // length개의 문자들을 제거 | StringBuilder strBldr = new StringBuilder("Super Star"); strBldr.Remove(3, 5); Console.WriteLine(strBldr.ToString()); | Supar |
문자열의 일부를 다른 것으로 대체한다.
| StringBuilder Replace( char oldChar, // 를 char newChar) // 로 교체 | StringBuilder strBldr = new StringBuilder("Super Star"); strBldr.Replace('S', 's'); Console.WriteLine(strBldr.ToString()); | super star |
〃
| StringBuilder Replace( string oldValue, // 를 string newValue) // 로 교체 | StringBuilder strBldr = new StringBuilder( "One little, two little Indians"); strBldr.Replace("little", "big"); Console.WriteLine(strBldr.ToString()); | One big, two big Indians |
〃
| StringBuilder Replace( char oldChar, // 를 char newChar, // 로 교체 int startIndex, // 교체 범위의 시작 위치 int count) // startIndex ~ startIndex + count | StringBuilder strBldr = new StringBuilder( "One little, two little Indians"); strBldr.Replace('l', 'L', 0, 8); Console.WriteLine(strBldr.ToString()); | One Little, two little Indians |
〃
| StringBuilder Replace( string oldValue, // 를 string newValue, // 로 교체 int startIndex, // 교체 범위의 시작 위치 int count) // startIndex ~ startIndex + count | StringBuilder strBldr = new StringBuilder( "One little, two little Indians"); strBldr.Replace("little", "big", 15, 10); Console.WriteLine(strBldr.ToString()); | One little, two big Indians |
StringBuilder 객체를 string 형으로 바꾼다.
| string ToString() | StringBuilder strBldr = new StringBuilder("Hello."); Console.WriteLine(strBldr.ToString()); | Hello. |
형식에 맞춘 문자열을 추가하고 싶다. | AppendFormat( string format, params Object[] args) | string name1 = "Park", name2 = "Son"; StringBuilder strBldr = new StringBuilder( "One little, two little Indians"); strBldr.AppendFormat( "\nTheir names: {0}, {1}", name1, name2 ); Console.WriteLine(strBldr.ToString()); | One little, two little Indians Their names: Park, Son |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| class StBuilder { public static void Main() { StringBuilder sb = new StringBuilder( "Hello" ); Console.WriteLine( "문자열 {0}" , sb); //capacity - 객체의 용량 반환, 50으로 설정해도 자동으로 64로 됨 Console.WriteLine( "해시코드 : {0}, 전체공간 : {1}, 문자열 길이:{2} " , sb.GetHashCode(), sb.Capacity, sb.Length); sb.Append( "World!!~!~!~~!!!" ); Console.WriteLine( "문자열 {0}" , sb); Console.WriteLine( "해시코드 : {0}, 전체공간 : {1}, 문자열 길이:{2} " , sb.GetHashCode(), sb.Capacity, sb.Length); sb.EnsureCapacity(50); //string 객체를 배열처럼 다룰수도 있음 sb[0] = 'Y' ; sb[6] = 'p' ; Console.WriteLine( "h-y, y-p 변경 : {0}" , sb.ToString()); //전체 문자열 Console.WriteLine( "해시코드 : {0}, 전체공간 : {1}, 문자열 길이:{2}, 최대 가능 공간 : {3} " , sb.GetHashCode(), sb.Capacity, sb.Length, sb.MaxCapacity); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| using System; using System.Text; using System.Diagnostics; class Program { static void Main() { // 1. // Declare a new StringBuilder. StringBuilder builder = new StringBuilder(); // 2. builder.Append( "The list starts here:" ); // 3. builder.AppendLine(); // 4. builder.Append( "1 cat" ).AppendLine(); // 5. // Get a reference to the StringBuilder's buffer content. string innerString = builder.ToString(); // Display with Debug. Debug.WriteLine(innerString); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| using System; using System.Text; class Program { static void Main() { StringBuilder builder = new StringBuilder( "This is an example string that is an example." ); builder.Replace( "an" , "the" ); // Replaces 'an' with 'the'. Console.WriteLine(builder.ToString()); Console.ReadLine(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| using System; using System.Text; class Program { static void Main() { string [] items = { "Cat" , "Dog" , "Celebrity" }; StringBuilder builder2 = new StringBuilder( "These items are required:" ).AppendLine(); foreach ( string item in items) { builder2.Append(item).AppendLine(); } Console.WriteLine(builder2.ToString()); Console.ReadLine(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| using System; using System.Text; class Program { static string [] _items = new string [] { "cat" , "dog" , "giraffe" }; /// <summary> /// Append to a new StringBuilder and return it as a string. /// </summary> static string A1() { StringBuilder b = new StringBuilder(); foreach ( string item in _items) { b.AppendLine(item); } return b.ToString(); } static void Main() { // Called in loop. A1(); } } |
댓글