.net 常用的一些实用方法

View Code
  1    #region 将字符串转换为数组
2 public static string[] GetStrArray(string str)
3 {
4 return str.Split(new char[',']);
5 }
6 #endregion
7
8 #region 将数组转换为字符串
9 public static string GetArrayStr(List<string> list, string speater)
10 {
11 StringBuilder sb = new StringBuilder();
12 for (int i = 0; i < list.Count; i++)
13 {
14 if (i == list.Count - 1)
15 {
16 sb.Append(list[i]);
17 }
18 else
19 {
20 sb.Append(list[i]);
21 sb.Append(speater);
22 }
23 }
24 return sb.ToString();
25 }
26 #endregion
27
28 #region 删除最后结尾的一个逗号
29 /// <summary>
30 /// 删除最后结尾的一个逗号
31 /// </summary>
32 public static string DelLastComma(string str)
33 {
34 return str.Substring(0, str.LastIndexOf(","));
35 }
36 #endregion
37
38 #region 删除最后结尾的指定字符后的字符
39 /// <summary>
40 /// 删除最后结尾的指定字符后的字符
41 /// </summary>
42 public static string DelLastChar(string str, string strchar)
43 {
44 return str.Substring(0, str.LastIndexOf(strchar));
45 }
46 #endregion
47
48 #region 生成指定长度的字符串
49 /// <summary>
50 /// 生成指定长度的字符串,即生成strLong个str字符串
51 /// </summary>
52 /// <param name="strLong">生成的长度</param>
53 /// <param name="str">以str生成字符串</param>
54 /// <returns></returns>
55 public static string StringOfChar(int strLong, string str)
56 {
57 string ReturnStr = "";
58 for (int i = 0; i < strLong; i++)
59 {
60 ReturnStr += str;
61 }
62
63 return ReturnStr;
64 }
65 #endregion
66
67 #region 生成日期随机码
68 /// <summary>
69 /// 生成日期随机码
70 /// </summary>
71 /// <returns></returns>
72 public static string GetRamCode()
73 {
74 #region
75 return DateTime.Now.ToString("yyyyMMddHHmmssffff");
76 #endregion
77 }
78 #endregion
79
80 #region 截取字符长度
81 /// <summary>
82 /// 截取字符长度
83 /// </summary>
84 /// <param name="inputString">字符</param>
85 /// <param name="len">长度</param>
86 /// <returns></returns>
87 public static string CutString(string inputString, int len)
88 {
89 ASCIIEncoding ascii = new ASCIIEncoding();
90 int tempLen = 0;
91 string tempString = "";
92 byte[] s = ascii.GetBytes(inputString);
93 for (int i = 0; i < s.Length; i++)
94 {
95 if ((int)s[i] == 63)
96 {
97 tempLen += 2;
98 }
99 else
100 {
101 tempLen += 1;
102 }
103
104 try
105 {
106 tempString += inputString.Substring(i, 1);
107 }
108 catch
109 {
110 break;
111 }
112
113 if (tempLen > len)
114 break;
115 }
116 //如果截过则加上半个省略号
117 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
118 if (mybyte.Length > len)
119 tempString += "";
120 return tempString;
121 }
122 #endregion
123
124 #region 清除HTML标记
125 public static string DropHTML(string Htmlstring)
126 {
127 //删除脚本
128 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
129 //删除HTML
130 Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
131 Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
132 Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
133 Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
134 Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
135 Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
136 Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
137 Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
138 Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
139 Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
140 Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
141 Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
142 Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
143
144 Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
145 Htmlstring.Replace("<", "");
146 Htmlstring.Replace(">", "");
147 Htmlstring.Replace("\r\n", "");
148 Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
149 return Htmlstring;
150 }
151 #endregion
152
153 #region 清除HTML标记且返回相应的长度
154 public static string DropHTML(string Htmlstring, int strLen)
155 {
156 return CutString(DropHTML(Htmlstring), strLen);
157 }
158 #endregion
159
160 #region TXT代码转换成HTML格式
161 /// <summary>
162 /// 字符串字符处理
163 /// </summary>
164 /// <param name="chr">等待处理的字符串</param>
165 /// <returns>处理后的字符串</returns>
166 /// //把TXT代码转换成HTML格式
167 public static String ToHtml(string Input)
168 {
169 StringBuilder sb = new StringBuilder(Input);
170 sb.Replace("&", "&amp;");
171 sb.Replace("<", "&lt;");
172 sb.Replace(">", "&gt;");
173 sb.Replace("\r\n", "<br />");
174 sb.Replace("\n", "<br />");
175 sb.Replace("\t", " ");
176 //sb.Replace(" ", "&nbsp;");
177 return sb.ToString();
178 }
179 #endregion
180
181 #region HTML代码转换成TXT格式
182 /// <summary>
183 /// 字符串字符处理
184 /// </summary>
185 /// <param name="chr">等待处理的字符串</param>
186 /// <returns>处理后的字符串</returns>
187 /// //把HTML代码转换成TXT格式
188 public static String ToTxt(String Input)
189 {
190 StringBuilder sb = new StringBuilder(Input);
191 sb.Replace("&nbsp;", " ");
192 sb.Replace("<br>", "\r\n");
193 sb.Replace("<br>", "\n");
194 sb.Replace("<br />", "\n");
195 sb.Replace("<br />", "\r\n");
196 sb.Replace("&lt;", "<");
197 sb.Replace("&gt;", ">");
198 sb.Replace("&amp;", "&");
199 return sb.ToString();
200 }
201 #endregion
202
203 #region 检查危险字符
204 /// <summary>
205 /// 检查危险字符
206 /// </summary>
207 /// <param name="Input"></param>
208 /// <returns></returns>
209 public static string Filter(string sInput)
210 {
211 if (sInput == null || sInput == "")
212 return null;
213 string sInput1 = sInput.ToLower();
214 string output = sInput;
215 string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
216 if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
217 {
218 throw new Exception("字符串中含有非法字符!");
219 }
220 else
221 {
222 output = output.Replace("'", "''");
223 }
224 return output;
225 }
226 #endregion
227
228 #region 检查过滤设定的危险字符
229 /// <summary>
230 /// 检查过滤设定的危险字符
231 /// </summary>
232 /// <param name="InText">要过滤的字符串 </param>
233 /// <returns>如果参数存在不安全字符,则返回true </returns>
234 public static bool SqlFilter(string word, string InText)
235 {
236 if (InText == null)
237 return false;
238 foreach (string i in word.Split('|'))
239 {
240 if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
241 {
242 return true;
243 }
244 }
245 return false;
246 }
247 #endregion
248
249 #region 过滤特殊字符
250 /// <summary>
251 /// 过滤特殊字符
252 /// </summary>
253 /// <param name="Input"></param>
254 /// <returns></returns>
255 public static string Htmls(string Input)
256 {
257 if (Input != string.Empty && Input != null)
258 {
259 string ihtml = Input.ToLower();
260 ihtml = ihtml.Replace("<script", "&lt;script");
261 ihtml = ihtml.Replace("script>", "script&gt;");
262 ihtml = ihtml.Replace("<%", "&lt;%");
263 ihtml = ihtml.Replace("%>", "%&gt;");
264 ihtml = ihtml.Replace("<$", "&lt;$");
265 ihtml = ihtml.Replace("$>", "$&gt;");
266 return ihtml;
267 }
268 else
269 {
270 return string.Empty;
271 }
272 }
273 #endregion
274
275 #region 判断是否数字
276 /// <summary>
277 /// 名称:IsNumberic
278 /// 功能:判断输入的是否是数字
279 /// 参数:string oText:源文本
280 /// 返回值: bool true:是 false:否
281 /// </summary>
282 public static bool IsNumberic(string oText)
283 {
284 try
285 {
286 int var1 = Convert.ToInt32(oText);
287 return true;
288 }
289 catch
290 {
291 return false;
292 }
293 }
294 #endregion
295
296
297 /// <summary>
298 /// 获得当前绝对路径
299 /// </summary>
300 /// <param name="strPath">指定的路径</param>
301 /// <returns>绝对路径</returns>
302 public static string GetMapPath(string strPath)
303 {
304 if (strPath.ToLower().StartsWith("http://"))
305 {
306 return strPath;
307 }
308 if (HttpContext.Current != null)
309 {
310 return HttpContext.Current.Server.MapPath(strPath);
311 }
312 else //非web程序引用
313 {
314 strPath = strPath.Replace("/", "\\");
315 if (strPath.StartsWith("\\"))
316 {
317 strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
318 }
319 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
320 }
321 }
322
323
324 /// <summary>
325 /// 写cookie值
326 /// </summary>
327 /// <param name="strName">名称</param>
328 /// <param name="strValue"></param>
329 public static void WriteCookie(string strName, string strValue)
330 {
331 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
332 if (cookie == null)
333 {
334 cookie = new HttpCookie(strName);
335 }
336 cookie.Value = strValue;
337 HttpContext.Current.Response.AppendCookie(cookie);
338 }
339
340 /// <summary>
341 /// 写cookie值
342 /// </summary>
343 /// <param name="strName">名称</param>
344 /// <param name="strValue"></param>
345 public static void WriteCookie(string strName, string key, string strValue)
346 {
347 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
348 if (cookie == null)
349 {
350 cookie = new HttpCookie(strName);
351 }
352 cookie[key] = strValue;
353 HttpContext.Current.Response.AppendCookie(cookie);
354 }
355
356 /// <summary>
357 /// 写cookie值
358 /// </summary>
359 /// <param name="strName">名称</param>
360 /// <param name="strValue"></param>
361 /// <param name="strValue">过期时间(分钟)</param>
362 public static void WriteCookie(string strName, string strValue, int expires)
363 {
364 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
365 if (cookie == null)
366 {
367 cookie = new HttpCookie(strName);
368 }
369 cookie.Value = strValue;
370 cookie.Expires = DateTime.Now.AddMinutes(expires);
371 HttpContext.Current.Response.AppendCookie(cookie);
372 }
373
374 /// <summary>
375 /// 读cookie值
376 /// </summary>
377 /// <param name="strName">名称</param>
378 /// <returns>cookie值</returns>
379 public static string GetCookie(string strName)
380 {
381 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
382 return HttpContext.Current.Request.Cookies[strName].Value.ToString();
383
384 return "";
385 }
386
387 /// <summary>
388 /// 读cookie值
389 /// </summary>
390 /// <param name="strName">名称</param>
391 /// <returns>cookie值</returns>
392 public static string GetCookie(string strName, string key)
393 {
394 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
395 return HttpContext.Current.Request.Cookies[strName][key].ToString();
396
397 return "";
398 }

 

转载于:https://www.cnblogs.com/xunqi2012/archive/2012/01/29/2331142.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值