当前文件路径:AcomStore/Components/StrHelper.cs 
1
using System;
2
using System.Drawing;
3
using System.Web;
4
using System.Web.UI;
5
using System.Web.UI.WebControls;
6
using System.Text.RegularExpressions;
7
using System.IO;
8
using System.Drawing.Imaging;
9
using System.Text;
10
using System.Web.Security;
11
using System.Net;
12
using System.Collections;
13
//该源码下载自www.51aspx.com(51aspx.com)
14
15
using AcomLb.Components;
16
17
namespace AcomLb.Components
18
...{
19
public class StrHelper
20
...{
21
private static string passWord; //加密字符串
22
23
/**//// <summary>
24
/// 判断输入是否数字
25
/// </summary>
26
/// <param name="num">要判断的字符串</param>
27
/// <returns></returns>
28
static public bool VldInt(string num)
29
...{
30
...#region
31
int ResultNum;
32
return int.TryParse(num, out ResultNum);
33
#endregion
34
}
35
36
/**//// <summary>
37
/// 返回文本编辑器替换后的字符串
38
/// </summary>
39
/// <param name="str">要替换的字符串</param>
40
/// <returns></returns>
41
static public string GetHtmlEditReplace(string str)
42
...{
43
...#region
44
return str.Replace("'", "’").Replace(" ", " ").Replace(",", ",").Replace("%", "%").
45
Replace("script", "").Replace(".js", "");
46
#endregion
47
}
48
49
/**//// <summary>
50
/// 截取字符串函数
51
/// </summary>
52
/// <param name="str">所要截取的字符串</param>
53
/// <param name="num">截取字符串的长度</param>
54
/// <returns></returns>
55
static public string GetSubString(string str, int num)
56
...{
57
...#region
58
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
59
#endregion
60
}
61
62
/**//// <summary>
63
/// 截取字符串优化版
64
/// </summary>
65
/// <param name="stringToSub">所要截取的字符串</param>
66
/// <param name="length">截取字符串的长度</param>
67
/// <returns></returns>
68
public static string GetFirstString(string stringToSub, int length)
69
...{
70
...#region
71
Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
72
char[] stringChar = stringToSub.ToCharArray();
73
StringBuilder sb = new StringBuilder();
74
int nLength = 0;
75
bool isCut = false;
76
for (int i = 0; i < stringChar.Length; i++)
77
...{
78
if (regex.IsMatch((stringChar[i]).ToString()))
79
...{
80
sb.Append(stringChar[i]);
81
nLength += 2;
82
}
83
else
84
...{
85
sb.Append(stringChar[i]);
86
nLength = nLength + 1;
87
}
88
89
if (nLength > length)
90
...{
91
isCut = true;
92
break;
93
}
94
}
95
if (isCut)
96
return sb.ToString() + "..";
97
else
98
return sb.ToString();
99
#endregion
100
}
101
102
/**//// <summary>
103
/// 过滤输入信息
104
/// </summary>
105
/// <param name="text">内容</param>
106
/// <param name="maxLength">最大长度</param>
107
/// <returns></returns>
108
public static string InputText(string text, int maxLength)
109
...{
110
...#region
111
text = text.Trim();
112
if (string.IsNullOrEmpty(text))
113
return string.Empty;
114
if (text.Length > maxLength)
115
text = text.Substring(0, maxLength);
116
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
117
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
118
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
119
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
120
text = text.Replace("'", "''");
121
return text;
122
#endregion
123
}
124
125
/**//// <summary>
126
/// 生成随机数
127
/// </summary>
128
/// <returns></returns>
129
private string GenerateCheckCode()
130
...{
131
...#region
132
int number;
133
char code;
134
string checkCode = String.Empty;
135
136
System.Random random = new Random();
137
138
for (int i = 0; i < 5; i++)
139
...{
140
number = random.Next();
141
142
if (number % 2 == 0)
143
code = (char)('0' + (char)(number % 10));
144
else
145
code = (char)('A' + (char)(number % 26));
146
147
checkCode += code.ToString();
148
}
149
150
HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
151
152
return checkCode;
153
#endregion
154
}
155
156
157
/**//// <summary>
158
/// 生成验证码图片
159
/// </summary>
160
public void CreateCheckCodeImage()
161
...{
162
...#region
163
string checkCode = GenerateCheckCode();
164
if (checkCode == null || checkCode.Trim() == String.Empty)
165
return;
166
167
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
168
Graphics g = Graphics.FromImage(image);
169
170
try
171
...{
172
//生成随机生成器
173
Random random = new Random();
174
175
//清空图片背景色
176
g.Clear(Color.White);
177
178
//画图片的背景噪音线
179
for (int i = 0; i < 25; i++)
180
...{
181
int x1 = random.Next(image.Width);
182
int x2 = random.Next(image.Width);
183
int y1 = random.Next(image.Height);
184
int y2 = random.Next(image.Height);
185
186
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
187
}
188
189
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
190
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
191
g.DrawString(checkCode, font, brush, 2, 2);
192
193
//画图片的前景噪音点
194
for (int i = 0; i < 150; i++)
195
...{
196
int x = random.Next(image.Width);
197
int y = random.Next(image.Height);
198
199
image.SetPixel(x, y, Color.FromArgb(random.Next()));
200
}
201
202
//画图片的边框线
203
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
204
205
System.IO.MemoryStream ms = new System.IO.MemoryStream();
206
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
207
HttpContext.Current.Response.ClearContent();
208
HttpContext.Current.Response.ContentType = "image/Gif";
209
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
210
}
211
finally
212
...{
213
g.Dispose();
214
image.Dispose();
215
}
216
#endregion
217
}
218
219
生成指定位数随机数#region 生成指定位数随机数
220
private static char[] constant =
221
...{
222
'0','1','2','3','4','5','6','7','8','9',
223
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
224
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
225
};
226
227
public static string GenerateRandom(int Length)
228
...{
229
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
230
Random rd = new Random();
231
for (int i = 0; i < Length; i++)
232
...{
233
newRandom.Append(constant[rd.Next(62)]);
234
}
235
return newRandom.ToString();
236
}
237
238
public static string GetNumRandom(int Length)
239
...{
240
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(10);
241
char[] NumStr=...{'0','1','2','3','4','5','6','7','8','9'};
242
Random rd = new Random();
243
for (int i = 0; i < Length; i++)
244
...{
245
newRandom.Append(constant[rd.Next(10)]);
246
}
247
return newRandom.ToString();
248
}
249
#endregion
250
251
/**//// <summary>
252
/// 获取汉字第一个拼音
253
/// </summary>