c#字符类型判断

 1        /// <summary>
 2         /// 判断是否为空字符串
 3         /// </summary>
 4         /// <param name="p_Source">需要判断的字符串</param> 
 5         /// <remarks>全为空格的字符串(包括全角空格和半角)或字符串为 null 返回 true,否则返回false</remarks>
 6         /// <returns>true: 空字符串, false 非空字符串
 7         /// </returns>
 8         /// <seealso cref="IsNotNullString"/>
 9         public static bool IsNullString(string p_Source)
10         {
11             if (p_Source == null)
12             {
13                 return true;
14             }
15             p_Source = p_Source.Trim(' ', ' ');
16             if (p_Source == String.Empty)
17             {
18                 return true;
19             }
20             return false;
21         }
22 
23         /// <summary>
24         /// 判断是否不为空字符串
25         /// </summary>
26         /// <param name="p_Source">需要判断的字符串</param> 
27         /// <returns></returns>
28         public static bool IsNotNullString(string p_Source)
29         {
30             return !IsNullString(p_Source);
31         }
32 
33         /// <summary>
34         /// 判断是否为空
35         /// </summary>
36         /// <param name="str">待检查数据。"",&nbsp;,null,&nbsp,undefined,Length == 0</param>
37         /// <returns></returns>
38         public static bool IsEmpty(string str)
39         {
40             if (string.IsNullOrEmpty(str) || str == "&nbsp;" || str == "&nbsp" || str.Length == 0 || str == "undefined") return true;
41 
42             return false;
43         }
44 
45         /// <summary>
46         /// 判断输入是否为日期类型
47         /// </summary>
48         /// <param name="strSrc">待检查数据 </param>
49         /// <returns></returns>
50         public static bool IsDate(string strSrc)
51         {
52             try
53             {
54                 DateTime d = DateTime.Parse(strSrc);
55                 return true;
56             }
57             catch
58             {
59                 return false;
60             }
61         }
62 
63         /// <summary>
64         /// 判断是否为数字
65         /// </summary>
66         /// <param name="strSrc">待检查数据</param>
67         /// <returns></returns>
68         public static bool IsNumeric(string strSrc)
69         {
70             if (strSrc != null && Regex.IsMatch(strSrc, @"^\d+$")) return true;
71             return false;
72         }

 

 1         /// <summary>
 2         /// 将字符串转换成 html 编码字符
 3         /// </summary>
 4         /// <param name="str"></param>
 5         /// <returns></returns>
 6         public static string HtmllEncode(string str)
 7         {
 8             return System.Web.HttpUtility.HtmlEncode(str);
 9         }
10 
11         /// <summary>
12         /// 将 html 解码
13         /// </summary> 
14         /// <param name="str">要转换的字符串</param>
15         /// <returns></returns>
16         public static string HtmlDecode(string str)
17         {
18             return System.Web.HttpUtility.HtmlDecode(str);
19         }

 

posted @ 2013-03-15 10:42  赤日枫叶  阅读(835)  评论(0编辑  收藏  举报