// string CreateRandomCode(int codeCount) 根据长度生成随机的数字和字母
// bool toFilter(string thePara) 检测非法字符,如果参数是空/包含非法字符,返回false/否则返回 true
// bool CheckNumber(string GetNum) 判断是否是数字格式
// bool CheckNumberRegx(string GetNum) 判断是否是正负数字含小数
// bool CheckNullstr(string Getstr) 判断是否是空值null 返回true || false
这是我项目里面用到的用来检测或是处理字符,随机生成数字等的一个通用类文件:
1
using System;
2
using System.Collections.Generic;
3
using System.Text.RegularExpressions;
4
using System.Text;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace InsApp.word
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// string CreateRandomCode(int codeCount) 根据长度生成随机的数字和字母
11
/// bool toFilter(string thePara) 检测非法字符,如果参数是空/包含非法字符,返回false/否则返回 true
12
/// bool CheckNumber(string GetNum) 判断是否是数字格式
13
/// bool CheckNumberRegx(string GetNum) 判断是否是正负数字含小数
14
/// bool CheckNullstr(string Getstr) 判断是否是空值null 返回true || false
15
/// </summary>
16
public class CreateCode
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
生成随机的数字和字母 codeCount是希望生成的长度#region 生成随机的数字和字母 codeCount是希望生成的长度
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
20
/// 生成随机的数字和字母
21
/// </summary>
22
/// <param name="codeCount">codeCount是希望生成的长度</param>
23
/// <returns></returns>
24
public string CreateRandomCode(int codeCount) //codeCount是希望生成的长度
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
string allChar = "0,1,2,3,4,5,6,7,8,9";
27
string[] allCharArray = allChar.Split(',');
28
string randomCode = "";
29
Random rand = new Random();
30
int temp = -1;
31
for (int i = 0; i < codeCount; i++)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
if (temp != -1)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
36
}
37
int t = rand.Next(0,10);
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
temp = t;
40
randomCode += allCharArray[t];
41
}
42
return randomCode;
43
}
44
#endregion
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
判断是否是数字格式#region 判断是否是数字格式
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
48
/// 判断是否是数字格式
49
/// </summary>
50
/// <param name="GetNum"></param>
51
public bool CheckNumber(string GetNum)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
Regex r = new Regex(@"^[0-9]+$");
55
if (r.IsMatch(GetNum))
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
return true;
58
}
59
else
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
return false;
62
}
63
}
64
#endregion
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
检测非法字符,防止sql注入#region 检测非法字符,防止sql注入
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
68
/// 检测非法字符,防止sql注入
69
/// 如果参数是空,返回false
70
/// 如果参数中包含非法字符,返回false
71
///// 否则返回 true
72
/// </summary>
73
/// <param name="thePara"></param>
74
/// <returns></returns>
75
public bool toFilter(string thePara)
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] BadCode = new string[]
{ "'", "\"", "exec", "cmd", ">", "<", "and", "=", "\\", ";" };
78
try
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
if (CheckNullstr(thePara) == false) //如果参数是空值,返回false
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
throw new Exception("参数为空");
83
}
84
else
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
for (int i = 0; i < BadCode.Length; i++)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
if (thePara.IndexOf(BadCode[i]) > 0)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
throw new Exception("包含非法字符");
91
}
92
}
93
}
94
return true;
95
}
96
catch
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
98
return false;
99
}
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
}
103
#endregion
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
bool CheckNullstr(string Getstr)判断是否是空值#region bool CheckNullstr(string Getstr)判断是否是空值
106![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
107
/// Getstr得到参数判断是否是空值
108
/// </summary>
109
/// <param name="Getstr">需要检查的值</param>
110
/// <param name="GetShow">这个字段的功能说明:姓名,sex</param>
111
public bool CheckNullstr(string Getstr)
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
113
try
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
if (Getstr == null || Getstr == "" || Getstr.Length < 1)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
return false;
118
}
119
else
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
return true;
122
}
123
}
124
catch
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
return false;
127
}
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
}
130
#endregion
131![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
bool CheckNumberRegx(string GetNum)正则表达式 判断是否是正负数字含小数#region bool CheckNumberRegx(string GetNum)正则表达式 判断是否是正负数字含小数
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
134
/// 判断是否是数字格式
135
/// </summary>
136
/// <param name="GetNum"></param>
137
public bool CheckNumberRegx(string GetNum)
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
//^[+-]?\d+(\.\d+)?$正负数字含小数 数字含小数^\d+(\.\d+)?$
140
Regex r = new Regex(@"^\d+(\.\d+)?$");
141
if (r.IsMatch(GetNum))
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
143
return true;
144
}
145
else
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
return false;
148
}
149
}
150
#endregion
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
用C#截取指定长度的中英文混合字符串#region 用C#截取指定长度的中英文混合字符串
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
154
/// s接受的字符
155
/// l长度
156
/// </summary>
157
/// <param name="s"></param>
158
/// <param name="l"></param>
159
/// <returns></returns>
160
public static string CutStr(string s, int l)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
string temp = s;
163
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165
return temp;
166
}
167
for (int i = temp.Length; i >= 0; i--)
168![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
169
temp = temp.Substring(0, i);
170
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
171![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
172
return temp + "";
173
}
174
}
175
return "";
176
}
177
#endregion
178![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
179![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
数字和字母随机数#region 数字和字母随机数
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
181
/// 数字和字母随机数
182
/// </summary>
183
/// <param name="n">生成长度</param>
184
/// <returns></returns>
185
public static string Rand_Number_AZ_Code(int n)
186![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
187![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] arrChar = new char[]
{
188
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
189
'0','1','2','3','4','5','6','7','8','9',
190
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
191
};
192![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
193
StringBuilder num = new StringBuilder();
194![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
195
Random rnd = new Random(DateTime.Now.Millisecond);
196
for (int i = 0; i < n; i++)
197![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
198
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
199
}
200![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
201
return num.ToString();
202
}
203
#endregion
204![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
205![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字母随机数#region 字母随机数
206![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
207
/// 字母随机数
208
/// </summary>
209
/// <param name="n">生成长度</param>
210
/// <returns></returns>
211
public static string RandLetter(int n)
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
213![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] arrChar = new char[]
{
214
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
215
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
216
};
217![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
218
StringBuilder num = new StringBuilder();
219![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
220
Random rnd = new Random(DateTime.Now.Millisecond);
221
for (int i = 0; i < n; i++)
222![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
223
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225
}
226![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
227
return num.ToString();
228
}
229
#endregion
230![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
231![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
日期随机函数#region 日期随机函数
232![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
233
/// 日期随机函数
234
/// </summary>
235
/// <param name="ra">长度</param>
236
/// <returns></returns>
237
public static string DateRndName(Random ra)
238![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
239
DateTime d = DateTime.Now;
240
string s = null, y, m, dd, h, mm, ss;
241
y = d.Year.ToString();
242
m = d.Month.ToString();
243
if (m.Length < 2) m = "0" + m;
244
dd = d.Day.ToString();
245
if (dd.Length < 2) dd = "0" + dd;
246
h = d.Hour.ToString();
247
if (h.Length < 2) h = "0" + h;
248
mm = d.Minute.ToString();
249
if (mm.Length < 2) mm = "0" + mm;
250
ss = d.Second.ToString();
251
if (ss.Length < 2) ss = "0" + ss;
252
s += y + m + dd + h + mm + ss;
253
s += ra.Next(100, 999).ToString();
254
return s;
255
}
256
#endregion
257![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
258![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
生成GUID#region 生成GUID
259![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
260
/// 生成GUID
261
/// </summary>
262
/// <returns></returns>
263
public static string GetGuid()
264![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
265
System.Guid g = System.Guid.NewGuid();
266
return g.ToString();
267
}
268
#endregion
269
}
270
}
271![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
由枚举值生成数组,可以用于绑定到ComboBox上或者DropDownList。
class Utils
{
public static object[] GetEnumValues(Type type)
{
List<object> list = new List<object>();
for (int i = 0; ; i++)
{
string val = Enum.GetName(type, i);
if (!string.IsNullOrEmpty(val))
list.Add(val);
else
break;
}
return list.ToArray();
}
}
使用:
object[] list = Utils.GetEnumValues(typeof(ChartType))