using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace NetLibrary { public class DataConvert { public static DateTime? ToDateTime(string value) { if (value == null || value == "") return null; DateTime? dt = null; try { dt = Convert.ToDateTime(value); } catch { return null; } return dt; } public static Int32? ToInt32(string value) { if (value == null || value == "") return null; Int32? dt = null; try { dt = Convert.ToInt32(value); } catch { return null; } return dt; } public static Decimal? ToDecimal(string value) { if (value == null || value == "") return null; Decimal? dt = null; try { dt = Convert.ToDecimal(value); } catch { return null; } return dt; } public static Double? ToDouble(string value) { if (value == null || value == "") return null; Double? dt = null; try { dt = Convert.ToDouble(value); } catch { return null; } return dt; } public static Single? ToSingle(string value) { if (value == null || value == "") return null; Single? dt = null; try { dt = Convert.ToSingle(value); } catch { return null; } return dt; } public static Int16? ToInt16(string value) { if (value == null || value == "") return null; Int16? dt = null; try { dt = Convert.ToInt16(value); } catch { return null; } return dt; } public static String ToString(string value) { if (value == null || value == "") return ""; return value.Trim(); } #region 过滤网页标签 public static String CleanHTMLTag(string htmlStream) { if (!string.IsNullOrEmpty(htmlStream)) { /* * 最好把所有的特殊HTML标记都找出来,然后把与其相对应的Unicode字符一起影射到Hash表内,最后一起都替换掉 */ //先单独测试,成功后,再把所有模式合并 //注:这两个必须单独处理 //去掉嵌套了HTML标记的JavaScript:() //去掉css标记:() //去掉css标记:\\..*\\{[\\s\\S]*\\} htmlStream = Regex.Replace(htmlStream, "()|()", " ", RegexOptions.IgnoreCase); //htmlStream = RemoveTag(htmlStream, "script"); //htmlStream = RemoveTag(htmlStream, "style"); //去掉普通HTML标记:<[^>]+> //替换空格: |&|­| |­ htmlStream = Regex.Replace(htmlStream, "<[^>]+>| |&|­| |­|•|<|>", " ", RegexOptions.IgnoreCase); //htmlStream = RemoveTag(htmlStream); //替换左尖括号 //htmlStream = Regex.Replace(htmlStream, "<", "<"); //替换右尖括号 //htmlStream = Regex.Replace(htmlStream, ">", ">"); //替换空行 //htmlStream = Regex.Replace(htmlStream, "[\n|\r|\t]", " ");//[\n|\r][\t*| *]*[\n|\r] htmlStream = Regex.Replace(htmlStream, "(\r\n[\r|\n|\t| ]*\r\n)|(\n[\r|\n|\t| ]*\n)", "\r\n"); htmlStream = Regex.Replace(htmlStream, "[\t| ]{1,}", " "); } return htmlStream.Trim(); } #endregion #region 过滤网页代码 public static String ToCodeString(string value) { if (value == null || value == "") return ""; Dictionary where = new Dictionary(); where.Add(@"

", ""); where.Add(@"

", ""); where.Add(@"

", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"<.xml:namespace .+?>", ""); where.Add(@"", ""); where.Add(@"", ""); where.Add(@"\r", ""); where.Add(@"\n", ""); where.Add(@"\t", ""); where.Add(@" ", " "); where.Add(@"<", "<"); where.Add(@">", ">"); foreach (string s in where.Keys) { value = Regex.Replace(value, s, where[s], RegexOptions.IgnoreCase); } return value; } #endregion /// /// 转换空值 /// /// 校验值 /// 如果是空值,那么用这个值替换 /// public static object IsNull(object value, object defaultValue) { if (value != null && value != DBNull.Value) { return value; } else { return defaultValue; } } public static string Format(object value, TypeCode type, string format) { if (value == null || value.ToString() == "" || value == DBNull.Value) return ""; if (format == null || format == "") return value.ToString(); switch (type) { case TypeCode.DateTime: return Convert.ToDateTime(value).ToString(format); case TypeCode.Decimal: return Convert.ToDecimal(value).ToString(format); case TypeCode.Double: return Convert.ToDouble(value).ToString(format); case TypeCode.Single: return Convert.ToSingle(value).ToString(format); default: return ""; } } public static string Format(object value, string format) { if (value == null || value.ToString() == "" || value == DBNull.Value) return ""; if (format == null || format == "") return value.ToString(); try { return Convert.ToDecimal(value).ToString(format); } catch { } try { return Convert.ToDateTime(value).ToString(format); } catch { } return value.ToString(); } } }