استفاده از StringBuilder در .net
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
using System;
using System.Text;
public class Example
{
public static void Main()
{
var sb = new StringBuilder();
ShowSbInfo(sb);
sb.Append("aaaaa");
ShowSbInfo(sb);
for (var ctr = 0; ctr <= 10; ctr++)
{
sb.Append("bbbbbbbbbbbbbbb");
ShowSbInfo(sb);
}
Console.WriteLine("------------------");
Console.WriteLine(sb.Capacity);
sb.EnsureCapacity(240);
Console.WriteLine(sb.Capacity);
sb.EnsureCapacity(280);
Console.WriteLine(sb.Capacity);
sb.EnsureCapacity(int.MaxValue);
Console.WriteLine(sb.Capacity);
Console.WriteLine("------------------");
Console.WriteLine(sb.MaxCapacity);
Console.WriteLine(sb.Length);
Console.WriteLine("------------------");
var chunk = sb.GetChunks();
foreach (var item in chunk)
{
Console.WriteLine(item);
}
Console.WriteLine("------------------");
var sb2 = new StringBuilder(10, 30);
sb2.Append("aaaaa");
sb2.Append("aaaaa");
sb2.Append("aaaaa");
sb2.Append("aaaaaaaaaa");
sb2.Append("aaaaaaaaaa");
//sb2.Append("aaaaaaaaaa");
Console.WriteLine(sb2.Capacity);
Console.WriteLine(sb2.Length);
}
private static void ShowSbInfo(StringBuilder sb)
{
foreach (var prop in sb.GetType().GetProperties())
{
if (prop.GetIndexParameters().Length == 0)
Console.Write("{0}: {1:N0} ", prop.Name, prop.GetValue(sb));
}
Console.WriteLine();
}
}
اطلاعات بیشتر: