2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4using System.Text;
5
6
7namespace 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
46 判断是否是数字格式
65
66 检测非法字符,防止sql注入
104
105 bool CheckNullstr(string Getstr)判断是否是空值
131
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
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
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
193 StringBuilder num = new StringBuilder();
194
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
201 return num.ToString();
202 }
203 #endregion
204
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
218 StringBuilder num = new StringBuilder();
219
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
225 }
226
227 return num.ToString();
228 }
229 #endregion
230
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
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
由枚举值生成数组,可以用于绑定到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))