This repository was archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtension.cs
97 lines (96 loc) · 5.67 KB
/
StringExtension.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomExtensions
{
public static class StringExtension
{
/// <summary>
/// 傳回新字串,此字串會以空白填補右側至指定的位元長度,靠左對齊這個字串中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元加上任何其他填補字元的位元長度。</param>
/// <returns>傳回填補後的新字串。</returns>
public static string PadRightBytes(this string str, int totalWidth)
{
return str.PadRightBytes(totalWidth, ' ');
}
/// <summary>
/// 傳回新字串,此字串會以指定的Unicode字元填補右側至指定的位元長度,靠左對齊這個字串中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元加上任何其他填補字元的位元長度。</param>
/// <param name="paddingChar">Unicode填補字元。</param>
/// <returns>傳回填補後的新字串。</returns>
public static string PadRightBytes(this string str, int totalWidth, char paddingChar)
{
return str.PadRight(totalWidth - System.Text.Encoding.Default.GetBytes(str).Length + str.Length, paddingChar);
}
/// <summary>
/// 傳回新字串,此字串會以空白填補左側至指定的位元長度,靠右對齊這個執行個體中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元加上任何其他填補字元的位元長度。</param>
/// <param name="paddingChar">Unicode填補字元。</param>
/// <returns>傳回填補後的新字串。</returns>
public static string PadLeftBytes(this string str, int totalWidth)
{
return str.PadLeftBytes(totalWidth, ' ');
}
/// <summary>
/// 傳回新字串,此字串會以指定的Unicode字元填補左側至指定的位元長度,靠右對齊這個執行個體中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元加上任何其他填補字元的位元長度。</param>
/// <param name="paddingChar">Unicode填補字元。</param>
/// <returns>傳回填補後的新字串。</returns>
public static string PadLeftBytes(this string str, int totalWidth, char paddingChar)
{
return str.PadLeft(totalWidth - System.Text.Encoding.Default.GetBytes(str).Length + str.Length, paddingChar);
}
/// <summary>
/// 傳回新字串,此字串會從右側切除超出位元長度的Unicode字元,並以空白填補右側至指定的位元長度,靠左對齊這個字串中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元減去超出字元後加上任何其他填補字元的位元長度。</param>
/// <returns>傳回對齊後的新字串。</returns>
public static string AlignRightBytes(this string str, int totalWidth)
{
return str.AlignRightBytes(totalWidth, ' ');
}
/// <summary>
/// 傳回新字串,此字串會從右側切除超出位元長度的Unicode字元,並以指定的Unicode字元填補右側至指定的位元長度,靠左對齊這個字串中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元減去超出字元後加上任何其他填補字元的位元長度。</param>
/// <param name="paddingChar">Unicode填補字元。</param>
/// <returns>傳回對齊後的新字串。</returns>
public static string AlignRightBytes(this string str, int totalWidth, char paddingChar)
{
while (System.Text.Encoding.Default.GetBytes(str).Length > totalWidth)
{
str = str.Remove(str.Length - 1);
}
return str.PadRightBytes(totalWidth, paddingChar);
}
/// <summary>
/// 傳回新字串,此字串會從右側切除超出位元長度的Unicode字元,並以空白填補左側至指定的位元長度,靠右對齊這個執行個體中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元減去超出字元後加上任何其他填補字元的位元長度。</param>
/// <param name="paddingChar">Unicode填補字元。</param>
/// <returns>傳回對齊後的新字串。</returns>
public static string AlignLeftBytes(this string str, int totalWidth)
{
return str.AlignLeftBytes(totalWidth, ' ');
}
/// <summary>
/// 傳回新字串,此字串會從右側切除超出位元長度的Unicode字元,並以指定的Unicode字元填補左側至指定的位元長度,靠右對齊這個執行個體中的字元。
/// </summary>
/// <param name="totalWidth">產生的字串中的位元長度,等於原始字元減去超出字元後加上任何其他填補字元的位元長度。</param>
/// <param name="paddingChar">Unicode填補字元。</param>
/// <returns>傳回對齊後的新字串。</returns>
public static string AlignLeftBytes(this string str, int totalWidth, char paddingChar)
{
while (System.Text.Encoding.Default.GetBytes(str).Length > totalWidth)
{
str = str.Remove(0, 1);
}
return str.PadLeftBytes(totalWidth, paddingChar);
}
}
}