본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - StringBuilder 사용법 (스트링빌더 / Append / Insert / Replace / Remove / ToString / Clear)

728x90
반응형


 WPF & C# - StringBuilder 사용법 (스트링빌더 / Append / Insert / Replace / Remove / ToString / Clear)



Test_StringBuilder.zip




MainWindow.xaml



1
2
<Button x:Name="btn" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="btn_Click"/>
<Label x:Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="90,7,0,0" VerticalAlignment="Top"/>
cs




MainWindow.xaml.cs



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
lbl.Content = "StringBuilder 객체 생성" + "\n\n";
StringBuilder stb = new StringBuilder("ABC");
lbl.Content += stb + "\n\n";
 
// Append()
lbl.Content += "===== Append() 메소드 ===== ";
lbl.Content += "bool 추가" + "\n";
stb.Append(false);
lbl.Content += stb + "\n\n";
 
lbl.Content += "char 추가" + "\n";
stb.Append('D');
lbl.Content += stb + "\n\n";
 
lbl.Content += "char 반복 추가 (char만 가능)" + "\n";
stb.Append('E',3);
lbl.Content += stb + "\n\n";
 
lbl.Content += "string 추가" + "\n";
stb.Append("String_");
lbl.Content += stb + "\n\n";
 
lbl.Content += "string 부분 추가" + "\n";
stb.Append("String_",22);
lbl.Content += stb + "\n\n";
 
lbl.Content += "Int 추가" + "\n";
stb.Append(123);
lbl.Content += stb + "\n\n";
 
// Insert()
lbl.Content += "===== Insert() 메소드 ===== ";
lbl.Content += "맨 앞에 삽입" + "\n";
stb.Insert(0"맨앞");
lbl.Content += stb + "\n\n";
 
lbl.Content += "중간에 삽입" + "\n";
stb.Insert(5"중간");
lbl.Content += stb + "\n\n";
 
lbl.Content += "중간에 반복 삽입 (string만 가능)" + "\n";
stb.Insert(10"반복"3);
lbl.Content += stb + "\n\n";
 
lbl.Content += "맨끝에 삽입" + "\n";
stb.Insert(stb.Length, "맨끝");
lbl.Content += stb + "\n\n";
 
// Replace()
lbl.Content += "===== Replace() 메소드 ===== ";
lbl.Content += "Replace() 문자열 변경(치환)" + "\n";
stb.Replace("123""321");
lbl.Content += stb + "\n\n";
 
// Remove()
lbl.Content += "===== Remove() 메소드 ===== ";
lbl.Content += "Remove() 부분 제거" + "\n";
stb.Remove(13);
lbl.Content += stb + "\n\n";
 
// ToString()
lbl.Content += "===== ToString() 메소드 ===== ";
lbl.Content += "ToString() 객체를 string로 형변환" + "\n";
stb.ToString();
lbl.Content += stb + "\n\n";
 
// Clear()
lbl.Content += "===== Clear() 메소드 ===== ";
lbl.Content += "Clear() 모두 제거" + "\n";
stb.Clear();
lbl.Content += stb + "\n\n";
cs




728x90
반응형