You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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:(<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标记:<[^>]+>
//替换空格:&nbsp;|&amp;|&shy;|&#160;|&#173;
htmlStream = Regex.Replace(htmlStream, "<[^>]+>|&nbsp;|&amp;|&shy;|&#160;|&#173;|&bull;|&lt;|&gt;", " ", RegexOptions.IgnoreCase);
//htmlStream = RemoveTag(htmlStream);
//替换左尖括号
//htmlStream = Regex.Replace(htmlStream, "&lt;", "<");
//替换右尖括号
//htmlStream = Regex.Replace(htmlStream, "&gt;", ">");
//替换空行
//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(@"&nbsp;", " ");
where.Add(@"&lt;", "<");
where.Add(@"&gt;", ">");
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();
}
}
}