|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
namespace HuizhongLibrary
|
|
|
{
|
|
|
public class DataConvert
|
|
|
{
|
|
|
public static DateTime? ToDateTime(object val)
|
|
|
{
|
|
|
string value=Convert.ToString(val);
|
|
|
if (value == null || value == "") return null;
|
|
|
DateTime? dt = null;
|
|
|
try { dt = Convert.ToDateTime(value); }
|
|
|
catch { return null; }
|
|
|
return dt;
|
|
|
}
|
|
|
public static Int32? ToInt32(object val)
|
|
|
{
|
|
|
string value = Convert.ToString(val);
|
|
|
if (value == null || value == "") return null;
|
|
|
Int32? dt = null;
|
|
|
try { dt = Convert.ToInt32(value); }
|
|
|
catch { return null; }
|
|
|
return dt;
|
|
|
}
|
|
|
public static Decimal? ToDecimal(object val)
|
|
|
{
|
|
|
string value = Convert.ToString(val);
|
|
|
if (value == null || value == "") return null;
|
|
|
Decimal? dt = null;
|
|
|
try { dt = Convert.ToDecimal(value); }
|
|
|
catch { return null; }
|
|
|
return dt;
|
|
|
}
|
|
|
public static Double? ToDouble(object val)
|
|
|
{
|
|
|
string value = Convert.ToString(val);
|
|
|
if (value == null || value == "") return null;
|
|
|
Double? dt = null;
|
|
|
try { dt = Convert.ToDouble(value); }
|
|
|
catch { return null; }
|
|
|
return dt;
|
|
|
}
|
|
|
public static Single? ToSingle(object val)
|
|
|
{
|
|
|
string value = Convert.ToString(val);
|
|
|
if (value == null || value == "") return null;
|
|
|
Single? dt = null;
|
|
|
try { dt = Convert.ToSingle(value); }
|
|
|
catch { return null; }
|
|
|
return dt;
|
|
|
}
|
|
|
public static Int16? ToInt16(object val)
|
|
|
{
|
|
|
string value = Convert.ToString(val);
|
|
|
if (value == null || value == "") return null;
|
|
|
Int16? dt = null;
|
|
|
try { dt = Convert.ToInt16(value); }
|
|
|
catch { return null; }
|
|
|
return dt;
|
|
|
}
|
|
|
public static String ToString(object val)
|
|
|
{
|
|
|
string value = Convert.ToString(val);
|
|
|
if (value == null || value == "") return "";
|
|
|
return value.Trim();
|
|
|
}
|
|
|
|
|
|
#region 过滤网页标签
|
|
|
public static String CleanHTMLTag(string htmlStream)
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(htmlStream))
|
|
|
{
|
|
|
/*
|
|
|
* 最好把所有的特殊HTML标记都找出来,然后把与其相对应的Unicode字符一起影射到Hash表内,最后一起都替换掉
|
|
|
*/
|
|
|
|
|
|
//先单独测试,成功后,再把所有模式合并
|
|
|
|
|
|
//注:这两个必须单独处理
|
|
|
//去掉嵌套了HTML标记的JavaScript:(<script)[\\s\\S]*(</script>)
|
|
|
//去掉css标记:(<style)[\\s\\S]*(</style>)
|
|
|
//去掉css标记:\\..*\\{[\\s\\S]*\\}
|
|
|
htmlStream = Regex.Replace(htmlStream, "(<script)[\\s\\S]*?(</script>)|(<style)[\\s\\S]*?(</style>)", " ", 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<string, string> where = new Dictionary<string, string>();
|
|
|
where.Add(@"<P .+?>", "");
|
|
|
where.Add(@"<P>", "");
|
|
|
where.Add(@"</P>", "");
|
|
|
where.Add(@"<SPAN .+?>", "");
|
|
|
where.Add(@"<SPAN>", "");
|
|
|
where.Add(@"</SPAN>", "");
|
|
|
where.Add(@"<STRONG>", "");
|
|
|
where.Add(@"</STRONG>", "");
|
|
|
where.Add(@"<U>", "");
|
|
|
where.Add(@"</U>", "");
|
|
|
where.Add(@"<EM>", "");
|
|
|
where.Add(@"</EM>", "");
|
|
|
where.Add(@"<o:p>", "");
|
|
|
where.Add(@"</o:p>", "");
|
|
|
where.Add(@"<FONT .+?>", "");
|
|
|
where.Add(@"</FONT>", "");
|
|
|
where.Add(@"<I .+?>", "");
|
|
|
where.Add(@"</I>", "");
|
|
|
where.Add(@"<B .+?>", "");
|
|
|
where.Add(@"</B>", "");
|
|
|
where.Add(@"<.xml:namespace .+?>", "");
|
|
|
where.Add(@"<st1:chmetcnv .+?>", "");
|
|
|
where.Add(@"</st1:chmetcnv>", "");
|
|
|
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
|
|
|
|
|
|
/// <summary>
|
|
|
/// 转换空值
|
|
|
/// </summary>
|
|
|
/// <param name="value">校验值</param>
|
|
|
/// <param name="defaultValue">如果是空值,那么用这个值替换</param>
|
|
|
/// <returns></returns>
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 十六进制转换到十进制
|
|
|
/// </summary>
|
|
|
/// <param name="hex"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string Hex2Ten(string hex)
|
|
|
{
|
|
|
int ten = 0;
|
|
|
for (int i = 0, j = hex.Length - 1; i < hex.Length; i++)
|
|
|
{
|
|
|
ten += HexChar2Value(hex.Substring(i, 1)) * ((int)Math.Pow(16, j));
|
|
|
j--;
|
|
|
}
|
|
|
return ten.ToString();
|
|
|
}
|
|
|
|
|
|
public static int HexChar2Value(string hexChar)
|
|
|
{
|
|
|
switch (hexChar)
|
|
|
{
|
|
|
case "0":
|
|
|
case "1":
|
|
|
case "2":
|
|
|
case "3":
|
|
|
case "4":
|
|
|
case "5":
|
|
|
case "6":
|
|
|
case "7":
|
|
|
case "8":
|
|
|
case "9":
|
|
|
return Convert.ToInt32(hexChar);
|
|
|
case "a":
|
|
|
case "A":
|
|
|
return 10;
|
|
|
case "b":
|
|
|
case "B":
|
|
|
return 11;
|
|
|
case "c":
|
|
|
case "C":
|
|
|
return 12;
|
|
|
case "d":
|
|
|
case "D":
|
|
|
return 13;
|
|
|
case "e":
|
|
|
case "E":
|
|
|
return 14;
|
|
|
case "f":
|
|
|
case "F":
|
|
|
return 15;
|
|
|
default:
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 从十进制转换到十六进制
|
|
|
/// </summary>
|
|
|
/// <param name="ten"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string Ten2Hex(string ten)
|
|
|
{
|
|
|
ulong tenValue = Convert.ToUInt64(ten);
|
|
|
ulong divValue, resValue;
|
|
|
string hex = "";
|
|
|
do
|
|
|
{
|
|
|
//divValue = (ulong)Math.Floor(tenValue / 16);
|
|
|
|
|
|
divValue = (ulong)Math.Floor((decimal)(tenValue / 16));
|
|
|
|
|
|
resValue = tenValue % 16;
|
|
|
hex = tenValue2Char(resValue) + hex;
|
|
|
tenValue = divValue;
|
|
|
}
|
|
|
while (tenValue >= 16);
|
|
|
if (tenValue != 0)
|
|
|
hex = tenValue2Char(tenValue) + hex;
|
|
|
return hex;
|
|
|
}
|
|
|
|
|
|
public static string tenValue2Char(ulong ten)
|
|
|
{
|
|
|
switch (ten)
|
|
|
{
|
|
|
case 0:
|
|
|
case 1:
|
|
|
case 2:
|
|
|
case 3:
|
|
|
case 4:
|
|
|
case 5:
|
|
|
case 6:
|
|
|
case 7:
|
|
|
case 8:
|
|
|
case 9:
|
|
|
return ten.ToString();
|
|
|
case 10:
|
|
|
return "A";
|
|
|
case 11:
|
|
|
return "B";
|
|
|
case 12:
|
|
|
return "C";
|
|
|
case 13:
|
|
|
return "D";
|
|
|
case 14:
|
|
|
return "E";
|
|
|
case 15:
|
|
|
return "F";
|
|
|
default:
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|