NET-String操作的通用类
NET String操作的通用类

代码 1
public class StringPlus
2
{
3
public static List<string> GetStrArray(string str, char speater,bool toLower)
4
{
5
List<string> list = new List<string>();
6
string[] ss = str.Split(speater);
7
foreach (string s in ss)
8
{
9
if (!string.IsNullOrEmpty(s) &&s !=speater.ToString())
10
{
11
string strVal = s;
12
if (toLower)
13
{
14
strVal = s.ToLower();
15
}
16
list.Add(strVal);
17
}
18
}
19
return list;
20
}
21
public static string[] GetStrArray(string str)
22
{
23
return str.Split(new char[',']);
24
}
25
public static string GetArrayStr(List<string> list,string speater)
26
{
27
StringBuilder sb = new StringBuilder();
28
for (int i = 0; i < list.Count; i++)
29
{
30
if (i == list.Count - 1)
31
{
32
sb.Append(list[i]);
33
}
34
else
35
{
36
sb.Append(list[i]);
37
sb.Append(speater);
38
}
39
}
40
return sb.ToString();
41
}
42
43
44
删除最后一个字符之后的字符#region 删除最后一个字符之后的字符
45
46
/**//// <summary>
47
/// 删除最后结尾的一个逗号
48
/// </summary>
49
public static string DelLastComma(string str)
50
{
51
return str.Substring(0, str.LastIndexOf(","));
52
}
53
54
/**//// <summary>
55
/// 删除最后结尾的指定字符后的字符
56
/// </summary>
57
public static string DelLastChar(string str,string strchar)
58
{
59
return str.Substring(0, str.LastIndexOf(strchar));
60
}
61
62
#endregion
63
64
65
66
67
/**//// <summary>
68
/// 转全角的函数(SBC case)
69
/// </summary>
70
/// <param name="input"></param>
71
/// <returns></returns>
72
public static string ToSBC(string input)
73
{
74
//半角转全角:
75
char[] c = input.ToCharArray();
76
for (int i = 0; i < c.Length; i++)
77
{
78
if (c[i] == 32)
79
{
80
c[i] = (char)12288;
81
continue;
82
}
83
if (c[i] < 127)
84
c[i] = (char)(c[i] + 65248);
85
}
86
return new string(c);
87
}
88
89
/**//// <summary>
90
/// 转半角的函数(SBC case)
91
/// </summary>
92
/// <param name="input">输入</param>
93
/// <returns></returns>
94
public static string ToDBC(string input)
95
{
96
char[] c = input.ToCharArray();
97
for (int i = 0; i < c.Length; i++)
98
{
99
if (c[i] == 12288)
100
{
101
c[i] = (char)32;
102
continue;
103
}
104
if (c[i] > 65280 && c[i] < 65375)
105
c[i] = (char)(c[i] - 65248);
106
}
107
return new string(c);
108
}
109
110
public static List<string> GetSubStringList(string o_str, char sepeater)
111
{
112
List<string> list = new List<string>();
113
string[] ss = o_str.Split(sepeater);
114
foreach (string s in ss)
115
{
116
if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
117
{
118
list.Add(s);
119
}
120
}
121
return list;
122
}
123
124
125
将字符串样式转换为纯字符串#region 将字符串样式转换为纯字符串
126
public static string GetCleanStyle(string StrList, string SplitString)
127
{
128
string RetrunValue = "";
129
//如果为空,返回空值
130
if (StrList == null)
131
{
132
RetrunValue = "";
133
}
134
else
135
{
136
//返回去掉分隔符
137
string NewString = "";
138
NewString = StrList.Replace(SplitString, "");
139
RetrunValue = NewString;
140
}
141
return RetrunValue;
142
}
143
#endregion
144
145
将字符串转换为新样式#region 将字符串转换为新样式
146
public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
147
{
148
string ReturnValue = "";
149
//如果输入空值,返回空,并给出错误提示
150
if (StrList == null)
151
{
152
ReturnValue = "";
153
Error = "请输入需要划分格式的字符串";
154
}
155
else
156
{
157
//检查传入的字符串长度和样式是否匹配,如果不匹配,则说明使用错误。给出错误信息并返回空值
158
int strListLength = StrList.Length;
159
int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
160
if (strListLength != NewStyleLength)
161
{
162
ReturnValue = "";
163
Error = "样式格式的长度与输入的字符长度不符,请重新输入";
164
}
165
else
166
{
167
//检查新样式中分隔符的位置
168
string Lengstr = "";
169
for (int i = 0; i < NewStyle.Length; i++)
170
{
171
if (NewStyle.Substring(i, 1) == SplitString)
172
{
173
Lengstr = Lengstr + "," + i;
174
}
175
}
176
if (Lengstr != "")
177
{
178
Lengstr = Lengstr.Substring(1);
179
}
180
//将分隔符放在新样式中的位置
181
string[] str = Lengstr.Split(',');
182
foreach (string bb in str)
183
{
184
StrList = StrList.Insert(int.Parse(bb), SplitString);
185
}
186
//给出最后的结果
187
ReturnValue = StrList;
188
//因为是正常的输出,没有错误
189
Error = "";
190
}
191
}
192
return ReturnValue;
193
}
194
#endregion
195
}
196
public class StringPlus2

{3
public static List<string> GetStrArray(string str, char speater,bool toLower)4

{5
List<string> list = new List<string>();6
string[] ss = str.Split(speater);7
foreach (string s in ss)8

{9
if (!string.IsNullOrEmpty(s) &&s !=speater.ToString())10

{11
string strVal = s;12
if (toLower)13

{14
strVal = s.ToLower();15
}16
list.Add(strVal);17
}18
}19
return list;20
}21
public static string[] GetStrArray(string str)22

{23
return str.Split(new char[',']);24
}25
public static string GetArrayStr(List<string> list,string speater)26

{27
StringBuilder sb = new StringBuilder();28
for (int i = 0; i < list.Count; i++)29

{30
if (i == list.Count - 1)31

{32
sb.Append(list[i]);33
}34
else35

{36
sb.Append(list[i]);37
sb.Append(speater);38
}39
}40
return sb.ToString();41
}42
43
44

删除最后一个字符之后的字符#region 删除最后一个字符之后的字符45

46

/**//// <summary>47
/// 删除最后结尾的一个逗号48
/// </summary>49
public static string DelLastComma(string str)50

{51
return str.Substring(0, str.LastIndexOf(","));52
}53

54

/**//// <summary>55
/// 删除最后结尾的指定字符后的字符56
/// </summary>57
public static string DelLastChar(string str,string strchar)58

{59
return str.Substring(0, str.LastIndexOf(strchar));60
}61

62
#endregion63

64

65

66

67

/**//// <summary>68
/// 转全角的函数(SBC case)69
/// </summary>70
/// <param name="input"></param>71
/// <returns></returns>72
public static string ToSBC(string input)73

{74
//半角转全角:75
char[] c = input.ToCharArray();76
for (int i = 0; i < c.Length; i++)77

{78
if (c[i] == 32)79

{80
c[i] = (char)12288;81
continue;82
}83
if (c[i] < 127)84
c[i] = (char)(c[i] + 65248);85
}86
return new string(c);87
} 88

89

/**//// <summary>90
/// 转半角的函数(SBC case)91
/// </summary>92
/// <param name="input">输入</param>93
/// <returns></returns>94
public static string ToDBC(string input)95

{96
char[] c = input.ToCharArray();97
for (int i = 0; i < c.Length; i++)98

{99
if (c[i] == 12288)100

{101
c[i] = (char)32;102
continue;103
}104
if (c[i] > 65280 && c[i] < 65375)105
c[i] = (char)(c[i] - 65248);106
}107
return new string(c);108
}109

110
public static List<string> GetSubStringList(string o_str, char sepeater)111

{112
List<string> list = new List<string>();113
string[] ss = o_str.Split(sepeater);114
foreach (string s in ss)115

{116
if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())117

{118
list.Add(s);119
}120
}121
return list;122
}123

124

125

将字符串样式转换为纯字符串#region 将字符串样式转换为纯字符串126
public static string GetCleanStyle(string StrList, string SplitString)127

{128
string RetrunValue = "";129
//如果为空,返回空值130
if (StrList == null)131

{132
RetrunValue = "";133
}134
else135

{136
//返回去掉分隔符137
string NewString = "";138
NewString = StrList.Replace(SplitString, "");139
RetrunValue = NewString;140
}141
return RetrunValue;142
}143
#endregion144

145

将字符串转换为新样式#region 将字符串转换为新样式146
public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)147

{148
string ReturnValue = "";149
//如果输入空值,返回空,并给出错误提示150
if (StrList == null)151

{152
ReturnValue = "";153
Error = "请输入需要划分格式的字符串";154
}155
else156

{157
//检查传入的字符串长度和样式是否匹配,如果不匹配,则说明使用错误。给出错误信息并返回空值158
int strListLength = StrList.Length;159
int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;160
if (strListLength != NewStyleLength)161

{162
ReturnValue = "";163
Error = "样式格式的长度与输入的字符长度不符,请重新输入";164
}165
else166

{167
//检查新样式中分隔符的位置168
string Lengstr = "";169
for (int i = 0; i < NewStyle.Length; i++)170

{171
if (NewStyle.Substring(i, 1) == SplitString)172

{173
Lengstr = Lengstr + "," + i;174
}175
}176
if (Lengstr != "")177

{178
Lengstr = Lengstr.Substring(1);179
}180
//将分隔符放在新样式中的位置181
string[] str = Lengstr.Split(',');182
foreach (string bb in str)183

{184
StrList = StrList.Insert(int.Parse(bb), SplitString);185
}186
//给出最后的结果187
ReturnValue = StrList;188
//因为是正常的输出,没有错误189
Error = "";190
}191
}192
return ReturnValue;193
}194
#endregion195
}196

一个完整的人生应该是宽恕、容忍、等待和爱!

浙公网安备 33010602011771号