随机数生成、检测非法字符、判断数字格式、是否为空、枚举值转化为数组
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
{
9
/// <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
{
18
生成随机的数字和字母 codeCount是希望生成的长度
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
判断是否是数字格式
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
检测非法字符,防止sql注入
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
bool CheckNullstr(string Getstr)判断是否是空值
131![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
#region bool CheckNumberRegx(string GetNum)正则表达式 判断是否是正负数字含小数
133
/// <summary>
134
/// 判断是否是数字格式
135
/// </summary>
136
/// <param name="GetNum"></param>
137
public bool CheckNumberRegx(string GetNum)
138
{
139
//^[+-]?\d+(\.\d+)?$正负数字含小数 数字含小数^\d+(\.\d+)?$
140
Regex r = new Regex(@"^\d+(\.\d+)?$");
141
if (r.IsMatch(GetNum))
142
{
143
return true;
144
}
145
else
146
{
147
return false;
148
}
149
}
150
#endregion
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
#region 用C#截取指定长度的中英文混合字符串
153
/// <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
{
162
string temp = s;
163
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
164
{
165
return temp;
166
}
167
for (int i = temp.Length; i >= 0; i--)
168
{
169
temp = temp.Substring(0, i);
170
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
171
{
172
return temp + "";
173
}
174
}
175
return "";
176
}
177
#endregion
178![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
179
#region 数字和字母随机数
180
/// <summary>
181
/// 数字和字母随机数
182
/// </summary>
183
/// <param name="n">生成长度</param>
184
/// <returns></returns>
185
public static string Rand_Number_AZ_Code(int n)
186
{
187
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
{
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
#region 字母随机数
206
/// <summary>
207
/// 字母随机数
208
/// </summary>
209
/// <param name="n">生成长度</param>
210
/// <returns></returns>
211
public static string RandLetter(int n)
212
{
213
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
{
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
#region 日期随机函数
232
/// <summary>
233
/// 日期随机函数
234
/// </summary>
235
/// <param name="ra">长度</param>
236
/// <returns></returns>
237
public static string DateRndName(Random ra)
238
{
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
#region 生成GUID
259
/// <summary>
260
/// 生成GUID
261
/// </summary>
262
/// <returns></returns>
263
public static string GetGuid()
264
{
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)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
131
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
132
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
133
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
134
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
136
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
137
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
138
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
139
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
140
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
143
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
145
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
146
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
147
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
148
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
149
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
150
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
151
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
153
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
154
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
155
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
156
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
158
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
159
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
160
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
162
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
163
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
164
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
165
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
167
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
168
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
169
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
171
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
172
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
173
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
174
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
175
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
176
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
177
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
178
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
179
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
180
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
181
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
182
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
183
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
184
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
185
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
186
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
187
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
188
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
189
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
190
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
191
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
192
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
193
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
194
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
195
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
196
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
197
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
198
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
199
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
200
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
201
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
202
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
203
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
204
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
205
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
206
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
207
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
208
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
209
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
210
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
211
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
212
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
213
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
214
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
215
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
216
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
217
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
218
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
219
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
220
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
221
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
222
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
223
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
224
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
226
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
227
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
228
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
229
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
230
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
231
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
232
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
233
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
234
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
235
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
236
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
237
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
238
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
239
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
240
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
241
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
242
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
243
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
244
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
245
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
246
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
247
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
248
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
249
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
250
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
251
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
252
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
253
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
254
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
255
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
256
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
257
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
258
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
259
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
260
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
261
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
262
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
263
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
264
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
265
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
266
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
267
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
268
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
269
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
270
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
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();
}
}
{
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))