当前文件:
yyzyqBBS/COMP/StringUtil.cs[10K,2009-6-12 12:01:01],
打开代码结构图
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
12
namespace WeYyzyq.Comp
13
...{
14
/**//// <summary>
15
/// 对一些字符串进行操作的类
16
/// 创建时间:2006-8-3
17
/// 创建者:马先光
18
/// </summary>
19
public class StringUtil
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
try
32
...{
33
Convert.ToInt32(num);
34
return true;
35
}
36
catch ...{ return false; }
37
#endregion
38
}
39
40
/**//// <summary>
41
/// 返回文本编辑器替换后的字符串
42
/// </summary>
43
/// <param name="str">要替换的字符串</param>
44
/// <returns></returns>
45
static public string GetHtmlEditReplace(string str)
46
...{
47
...#region
48
return str.Replace("'", "''").Replace(" ", " ").Replace(",", ",").Replace("%", "%").Replace("script","").Replace(".js","");
49
#endregion
50
}
51
52
/**//// <summary>
53
/// 截取字符串函数
54
/// </summary>
55
/// <param name="str">所要截取的字符串</param>
56
/// <param name="num">截取字符串的长度</param>
57
/// <returns></returns>
58
static public string GetSubString(string str, int num)
59
...{
60
...#region
61
return (str.Length > num) ? str.Substring(0, num) + "..." : str;
62
#endregion
63
}
64
65
/**//// <summary>
66
/// 过滤输入信息
67
/// </summary>
68
/// <param name="text">内容</param>
69
/// <param name="maxLength">最大长度</param>
70
/// <returns></returns>
71
public static string InputText(string text, int maxLength)
72
...{
73
...#region
74
text = text.Trim();
75
if (string.IsNullOrEmpty(text))
76
return string.Empty;
77
if (text.Length > maxLength)
78
text = text.Substring(0, maxLength);
79
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
80
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
81
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
82
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
83
text = text.Replace("'", "''");
84
return text;
85
#endregion
86
}
87
88
/**//// <summary>
89
/// 生成随机数
90
/// </summary>
91
/// <returns></returns>
92
private string GenerateCheckCode()
93
...{
94
...#region
95
int number;
96
char code;
97
string checkCode = String.Empty;
98
99
System.Random random = new Random();
100
101
for (int i = 0; i < 5; i++)
102
...{
103
number = random.Next();
104
105
if (number % 2 == 0)
106
code = (char)('0' + (char)(number % 10));
107
else
108
code = (char)('A' + (char)(number % 26));
109
110
checkCode += code.ToString();
111
}
112
113
HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
114
115
return checkCode;
116
#endregion
117
}
118
119
120
/**//// <summary>
121
/// 生成验证码图片
122
/// </summary>
123
public void CreateCheckCodeImage()
124
...{
125
...#region
126
string checkCode = GenerateCheckCode();
127
if (checkCode == null || checkCode.Trim() == String.Empty)
128
return;
129
130
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
131
Graphics g = Graphics.FromImage(image);
132
133
try
134
...{
135
//生成随机生成器
136
Random random = new Random();
137
138
//清空图片背景色
139
g.Clear(Color.White);
140
141
//画图片的背景噪音线
142
for (int i = 0; i < 25; i++)
143
...{
144
int x1 = random.Next(image.Width);
145
int x2 = random.Next(image.Width);
146
int y1 = random.Next(image.Height);
147
int y2 = random.Next(image.Height);
148
149
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
150
}
151
152
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
153
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);
154
g.DrawString(checkCode, font, brush, 2, 2);
155
156
//画图片的前景噪音点
157
for (int i = 0; i < 100; i++)
158
...{
159
int x = random.Next(image.Width);
160
int y = random.Next(image.Height);
161
162
image.SetPixel(x, y, Color.FromArgb(random.Next()));
163
}
164
165
//画图片的边框线
166
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
167
168
System.IO.MemoryStream ms = new System.IO.MemoryStream();
169
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
170
HttpContext.Current.Response.ClearContent();
171
HttpContext.Current.Response.ContentType = "image/Gif";
172
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
173
}
174
finally
175
...{
176
g.Dispose();
177
image.Dispose();
178
}
179
#endregion
180
}
181
182
183
/**//// <summary>
184
/// 获取汉字第一个拼音
185
/// </summary>
186
/// <param name="input"></param>
187
/// <returns></returns>
188
static public string getSpells(string input)
189
...{
190
...#region
191
int len = input.Length;
192
string reVal = "";
193
for (int i = 0; i < len; i++)
194
...{
195
reVal += getSpell(input.Substring(i, 1));
196
}
197
return reVal;
198
#endregion
199
}
200
201
static public string getSpell(string cn)
202
...{
203
...#region
204
byte[] arrCN = Encoding.Default.GetBytes(cn);
205
if (arrCN.Length > 1)
206
...{
207
int area = (short)arrCN[0];
208
int pos = (short)arrCN[1];
209
int code = (area << 8) + pos;
210
int[] areacode = ...{ 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
211
for (int i = 0; i < 26; i++)
212
...{
213
int max = 55290;
214
if (i != 25) max = areacode[i + 1];
215
if (areacode[i] <= code && code < max)
216
...{
217
return Encoding.Default.GetString(new byte[] ...{ (byte)(65 + i) });
218
}
219
}
220
return "?";
221
}
222
else return cn;
223
#endregion
224
}
225
226
227
/**//// <summary>
228
/// 半角转全角
229
/// </summary>
230
/// <param name="BJstr"></param>
231
/// <returns></returns>
232
static public string GetQuanJiao(string BJstr)
233
...{
234
...#region
235
char[] c = BJstr.ToCharArray();
236
for (int i = 0; i < c.Length; i++)
237
...{
238
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
239
if (b.Length == 2)
240
...{
241
if (b[1] == 0)
242
...{
243
b[0] = (byte)(b[0] - 32);
244
b[1] = 255;
245
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
246
}
247
}
248
}
249
250
string strNew = new string(c);
251
return strNew;
252
253
#endregion
254
}
255
256
/**//// <summary>
257
/// 全角转半角
258
/// </summary>
259
/// <param name="QJstr"></param>
260
/// <returns></returns>
261
static public string GetBanJiao(string QJstr)
262
...{
263
...#region
264
char[] c = QJstr.ToCharArray();
265
for (int i = 0; i < c.Length; i++)
266
...{
267
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
268
if (b.Length == 2)
269
...{
270
if (b[1] == 255)
271
...{
272
b[0] = (byte)(b[0] + 32);
273
b[1] = 0;
274
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
275
}
276
}
277
}
278
string strNew = new string(c);
279
return strNew;
280
#endregion
281
}
282
283
加密的类型#region 加密的类型
284
/**//// <summary>
285
/// 加密的类型
286
/// </summary>
287
public enum PasswordType
288
...{
289
SHA1,
290
MD5
291
}
292
#endregion
293
294
295
/**//// <summary>
296
/// 字符串加密
297
/// </summary>
298
/// <param name="PasswordString">要加密的字符串</param>
299
/// <param name="PasswordFormat">要加密的类别</param>
300
/// <returns></returns>
301
static public string EncryptPassword(string PasswordString, string PasswordFormat)
302
...{
303
...#region
304
switch (PasswordFormat)
305
...{
306
case "SHA1":
307
...{
308
passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
309
break;
310
}
311
case "MD5":
312
...{
313
passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
314
break;
315
}
316
default:
317
...{
318
passWord = string.Empty;
319
break;
320
}
321
}
322
return passWord;
323
#endregion
324
}
325
326
}
327
}
328