using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Collections;
|
using System.Text.RegularExpressions;
|
using System.Web;
|
using DealMvc.Common.Net;
|
|
namespace CY.Infrastructure.Common
|
{
|
/// <summary>
|
/// 数据类型转换类
|
/// </summary>
|
public class MyConvert
|
{
|
#region string数据转换
|
public static bool GetBoolean(string value)
|
{
|
bool result = false;
|
bool.TryParse(value, out result);
|
return result;
|
}
|
|
public static DateTime GetDateTime(string value)
|
{
|
DateTime now = DateTime.Now;
|
DateTime.TryParse(value, out now);
|
return now;
|
}
|
|
public static decimal GetDecimal(string num)
|
{
|
decimal result = 0M;
|
decimal.TryParse(num, out result);
|
return result;
|
}
|
|
public static double GetDouble(string num)
|
{
|
double result = 0.0;
|
double.TryParse(num, out result);
|
return result;
|
}
|
|
public static float GetFloat(string num)
|
{
|
float result = 0f;
|
float.TryParse(num, out result);
|
return result;
|
}
|
|
public static short GetInt16(string num)
|
{
|
short result = 0;
|
short.TryParse(num, out result);
|
return result;
|
}
|
|
public static int GetInt32(string num)
|
{
|
int result = 0;
|
int.TryParse(num, out result);
|
return result;
|
}
|
|
public static long GetInt64(string num)
|
{
|
long result = 0L;
|
long.TryParse(num, out result);
|
return result;
|
}
|
|
#endregion
|
|
#region object数据转换
|
|
|
|
public static int? ConvertToInt(object targetValue)
|
{
|
int? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
int result = 0;
|
int.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static Int32? ConvertToInt32(object targetValue)
|
{
|
Int32? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
else { }
|
|
if (TypeParse.IsNumeric(targetValue))
|
return Int32.Parse(targetValue.ToString());
|
else
|
return null;
|
|
}
|
|
public static Int64? ConvertToInt64(object targetValue)
|
{
|
Int64? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
Int64 result = 0;
|
Int64.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static long? ConvertToLong(object targetValue)
|
{
|
long? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
long result = 0;
|
long.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static Boolean ConvertIntToBoolean(object targetValue)
|
{
|
return targetValue != DBNull.Value ? ("1".Equals(targetValue.ToString()) || true.ToString().Equals(targetValue.ToString())) : false;
|
}
|
/// <summary>
|
/// 转为Boolean
|
/// 2013-5-22 10:00修改实现为:true\"true"\"TRUE"\1 均为true
|
/// </summary>
|
/// <param name="targetValue"></param>
|
/// <returns></returns>
|
public static Boolean ConvertToBoolean(object targetValue)
|
{
|
return targetValue != DBNull.Value ? (true.ToString().ToUpper().Equals(targetValue.ToString().ToUpper()) || "1".Equals(targetValue)) : false;
|
}
|
|
public static Double? ConvertToDouble(object targetValue)
|
{
|
Double? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
Double result = 0;
|
Double.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static decimal? ConvertToDecimal(object targetValue)
|
{
|
decimal? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
decimal result = 0;
|
decimal.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static DateTime? ConvertToDateTime(object targetValue)
|
{
|
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return null;
|
}
|
else { }
|
|
string targetValueStr = targetValue.ToString();
|
|
if (string.IsNullOrEmpty(targetValueStr)) { return null; } else { }
|
|
DateTime returnValue;
|
DateTime.TryParse(targetValueStr, out returnValue);
|
|
return returnValue;
|
}
|
|
|
public static byte[] ConvertToByte(object targetValue)
|
{
|
return targetValue != DBNull.Value ? targetValue as byte[] : null;
|
}
|
|
public static Guid ConvertToGuid(object targetValue)
|
{
|
Guid outGuid = Guid.Empty;
|
Guid.TryParse(string.Format("{0}", targetValue), out outGuid);
|
return outGuid;
|
|
}
|
public static string ConvertToString(object targetValue)
|
{
|
return string.Format("{0}", targetValue);
|
}
|
#endregion
|
}
|
|
public static class StaticFunction
|
{
|
#region 常用方法
|
/// <summary>
|
/// object转GUID
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static Guid ToGuid2(this object obj)
|
{
|
Guid Val;
|
Val = MyConvert.ConvertToGuid(obj);
|
return Val;
|
}
|
|
|
/// <summary>
|
/// object转String,如何object为null返回Empty
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static string ToString2(this object obj)
|
{
|
//string Val = string.Empty;
|
//try { Val = obj.ToString(); }
|
//catch { Val = ""; }
|
return obj == null ? string.Empty : obj.ToString();
|
}
|
/// <summary>
|
/// object转Int,失败返回0
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static int? ToInt32(this object obj)
|
{
|
int? Val = 0;
|
if (null == obj || obj == DBNull.Value)
|
{
|
return null;
|
}
|
else
|
{
|
string objxx = obj.ToString();
|
if (objxx.IndexOf(".") >= 0)
|
{
|
obj = obj.ToString().Split('.')[0];
|
}
|
}
|
Val = MyConvert.ConvertToInt32(obj);
|
return Val;
|
}
|
/// <summary>
|
/// object转Double,失败返回0
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static double? ToDouble2(this object obj)
|
{
|
double? Val = 0;
|
Val = MyConvert.ConvertToDouble(obj);
|
return Val;
|
}
|
|
|
/// <summary>
|
/// object转Decimal,失败返回0
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static decimal? ToDecimal2(this object obj)
|
{
|
decimal? Val = 0;
|
Val = MyConvert.ConvertToDecimal(obj);
|
return Val;
|
}
|
|
/// <summary>
|
/// object转Double,失败返回0
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static string ToDecimal2Yen(this object obj)
|
{
|
decimal? Val = obj.ToDecimal2();
|
if (Val.HasValue)
|
{
|
return Val.Value.ToString("0.00");
|
}
|
else
|
{
|
return "0.00";
|
}
|
}
|
|
/// <summary>
|
/// object转DateTime
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static DateTime? ToDateTime2(this object obj)
|
{
|
return MyConvert.ConvertToDateTime(obj);
|
}
|
|
/// <summary>
|
/// DateTime?格式化,如何DateTime?为null返回Empty
|
/// </summary>
|
/// <param name="dt"></param>
|
/// <param name="format"></param>
|
/// <returns></returns>
|
public static string ToStringForDateTime(this DateTime? dt, string format)
|
{
|
string val = string.Empty;
|
try
|
{
|
val = ((DateTime)dt).ToString(format, System.Globalization.DateTimeFormatInfo.InvariantInfo);
|
}
|
catch
|
{
|
}
|
return val;
|
}
|
/// <summary>
|
/// object转Boolean,失败返回flase
|
/// </summary>
|
/// <param name="Str"></param>
|
/// <returns></returns>
|
public static bool ToBoolean2(this object obj)
|
{
|
bool Val = false;
|
try
|
{
|
Val = Convert.ToBoolean(obj);
|
}
|
catch
|
{
|
Val = false;
|
}
|
return Val;
|
}
|
/// <summary>
|
/// 判断URL参数是否为正整数
|
/// </summary>
|
/// <param name="param"></param>
|
public static bool isInt(this object param)
|
{
|
Regex regex = new Regex("(^0$)|(^-?[1-9][0-9]*$)");
|
if (string.IsNullOrEmpty(param.ToString2()))
|
return false;
|
if (regex.IsMatch(param.ToString2()))
|
return true;
|
else
|
return false;
|
}
|
/// <summary>
|
/// 判断参数是否为Double(比如价格)
|
/// </summary>
|
/// <param name="param">参数</param>
|
public static bool isDouble(this object param)
|
{
|
Regex regex3 = new Regex(@"(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\.\d*$)|(^0$)");
|
if (string.IsNullOrEmpty(param.ToString2()))
|
return false;
|
if (regex3.IsMatch(param.ToString2()))
|
return true;
|
else
|
return false;
|
}
|
/// <summary>
|
/// 检查中英文混合字符长度(英文字符算1,中文算2)
|
/// </summary>
|
/// <param name="source"></param>
|
/// <returns></returns>
|
public static int JStrLen(this string _String)
|
{
|
return DealString.ZhuanHuanStringASIIC(_String.ToString2()).Length;
|
}
|
/// <summary>
|
/// 截取字符串
|
/// </summary>
|
/// <param name="Str">源字符串</param>
|
/// <param name="length">保留长度后面加...</param>
|
/// <returns></returns>
|
public static string JSubString(this object Str, int length)
|
{
|
return Str.JSubString(length, "...");
|
}
|
/// <summary>
|
/// 截取字符串
|
/// </summary>
|
/// <param name="Str">源字符串</param>
|
/// <param name="length">保留长度</param>
|
/// <param name="Sig">后面的符号 例如...</param>
|
/// <returns></returns>
|
public static string JSubString(this object Str, int length, string Sig)
|
{
|
return DealString.getSubString(Str.ToString2(), length, Sig).JHtmlEncode();
|
}
|
/// <summary>
|
/// 去除字符串里面的HTML标签
|
/// </summary>
|
/// <param name="strHtml">字符串</param>
|
/// <returns></returns>
|
public static string JreplaceHTML(this object obj)
|
{
|
string strHtml = obj.ToString2();
|
return DealString.replaceHTML(strHtml);
|
}
|
/// <summary>
|
/// 去掉字符串里面的换行符
|
/// </summary>
|
/// <param name="Str">字符串</param>
|
/// <returns></returns>
|
public static string JclearString_R_N(this object Str)
|
{
|
return DealString.clearString_R_N(Str.ToString2());
|
}
|
/// <summary>
|
/// 把字符串转换成16进制
|
/// </summary>
|
/// <param name="mStr"></param>
|
/// <returns></returns>
|
public static string JStrToHex(this string mStr)
|
{
|
return DealString.StrToHex(mStr);
|
|
}
|
/// <summary>
|
/// 把16进制转换成字符串
|
/// </summary>
|
/// <param name="mHex"></param>
|
/// <returns></returns>
|
public static string JHexToStr(this string mHex)
|
{
|
return DealString.HexToStr(mHex);
|
}
|
/// <summary>
|
/// 对字符串进行md5加密
|
/// </summary>
|
/// <param name="_String">字符串对象</param>
|
/// <returns>加密有的字符串</returns>
|
public static string Jmd5(this string _String)
|
{
|
return DealString.MD5(_String);
|
}
|
/// <summary>
|
/// 取得此字符串的后缀名(针对字符串为文件名的情况,不包括.)
|
/// </summary>
|
/// <param name="_String">字符串对象</param>
|
/// <returns>截取后的子字符串</returns>
|
public static string JgetExtension(this string _String)
|
{
|
return DealString.getExtension(_String);
|
}
|
/// <summary>
|
/// 格式化字符串(,1,2,3,4,5,,2,32,,)为(1,2,3,4,5,2,32), 如果为空则返回字符串0
|
/// </summary>
|
/// <param name="Str">原字符串</param>
|
/// <param name="Sign">分割标识</param>
|
/// <param name="IsMustInt">必须为Int</param>
|
/// <returns></returns>
|
public static string JFormatIdsValues(this string Str, string Sign, bool IsMustInt)
|
{
|
string[] s = Str.Split(new string[] { Sign }, StringSplitOptions.RemoveEmptyEntries);
|
if (s.Length == 0)
|
return "";
|
ArrayList output = new ArrayList();
|
Regex reg = new Regex("^-?\\d+$");
|
foreach (string _s in s)
|
{
|
if (!string.IsNullOrEmpty(_s) && (reg.IsMatch(_s) || !IsMustInt))
|
output.Add(_s);
|
}
|
return output.JArrayListToString(Sign, true);
|
}
|
/// <summary>
|
/// 将双字节字符转换成 ww
|
/// </summary>
|
/// <param name="_string">待转换的字符串</param>
|
/// <returns>返回转换后的字符串</returns>
|
public static string JDoubleByteToTwoByte(this string Str)
|
{
|
Regex reg = new Regex(@"[^\x00-\xff]");
|
return reg.Replace(Str, "ww");
|
}
|
/// <summary>
|
/// 将已经为HTTP传输过的HTML编码字符串转换为已解码的字符串
|
/// </summary>
|
/// <param name="textToFormat"></param>
|
/// <returns></returns>
|
public static string JHtmlDecode(this string textToFormat)
|
{
|
if (string.IsNullOrEmpty(textToFormat))
|
{
|
return textToFormat;
|
}
|
return HttpUtility.HtmlDecode(textToFormat);
|
}
|
|
/// <summary>
|
/// 将字符串转换为html编码的字符串
|
/// </summary>
|
/// <param name="textToFormat"></param>
|
/// <returns></returns>
|
public static string JHtmlEncode(this object textToFormat)
|
{
|
string _textToFormat = textToFormat.ToString2();
|
if (string.IsNullOrEmpty(_textToFormat))
|
{
|
return _textToFormat;
|
}
|
return HttpUtility.HtmlEncode(_textToFormat);
|
}
|
|
/// <summary>
|
/// 把带有注入性的字符串转换成普通字符
|
/// </summary>
|
/// <param name="_string">待检查的字符串</param>
|
/// <returns></returns>
|
public static string FilterSQL(this string _string)
|
{
|
if (_string == null)
|
{
|
return null;
|
}
|
return DealString.ChangeSQL(_string);
|
}
|
#endregion
|
|
#region ArrayList Int[] String[] String
|
|
/// <summary>
|
/// ArrayList转字符串,每项以,分隔
|
/// </summary>
|
/// <param name="_ArrayList">源ArrayList</param>
|
/// <returns></returns>
|
public static string JArrayListToString(this ArrayList _ArrayList)
|
{
|
return JArrayListToString(_ArrayList, ",", false);
|
}
|
/// <summary>
|
/// ArrayList转字符串,每项以,分隔
|
/// </summary>
|
/// <param name="_ArrayList">源ArrayList</param>
|
/// <param name="RemoveEmptyEntries">是否去掉 空 值</param>
|
/// <returns></returns>
|
public static string JArrayListToString(this ArrayList _ArrayList, bool RemoveEmptyEntries)
|
{
|
return JArrayListToString(_ArrayList, ",", RemoveEmptyEntries);
|
}
|
/// <summary>
|
/// ArrayList转字符串,每项以Sig分隔
|
/// </summary>
|
/// <param name="_ArrayList">源ArrayList</param>
|
/// <param name="Sig">分隔符</param>
|
/// <returns></returns>
|
public static string JArrayListToString(this ArrayList _ArrayList, string Sig, bool RemoveEmptyEntries)
|
{
|
for (int i = 0; i < _ArrayList.Count; i++)
|
_ArrayList[i] = _ArrayList[i].ToString2();
|
|
string[] _s = (string[])_ArrayList.ToArray(typeof(string));
|
if (RemoveEmptyEntries)
|
{
|
string Mstr = string.Join(Sig, _s);
|
_s = Mstr.Split(new string[] { Sig }, StringSplitOptions.RemoveEmptyEntries);
|
}
|
return string.Join(Sig, _s);
|
}
|
/// <summary>
|
/// Double?[] 转 Double[]
|
/// </summary>
|
/// <param name="_Arr"></param>
|
/// <returns></returns>
|
public static double[] JDoublesNotHasNull(this double?[] _Arr)
|
{
|
ArrayList List = new ArrayList();
|
foreach (double? s in _Arr)
|
{
|
if (s != null)
|
List.Add(s);
|
}
|
return (double[])List.ToArray(typeof(double));
|
}
|
/// <summary>
|
/// Int?[] 转 Int[]
|
/// </summary>
|
/// <param name="_Arr"></param>
|
/// <returns></returns>
|
public static int[] JIntsNotHasNull(this int?[] _Arr)
|
{
|
ArrayList List = new ArrayList();
|
foreach (int? s in _Arr)
|
{
|
if (s != null)
|
List.Add(s);
|
}
|
return (int[])List.ToArray(typeof(int));
|
}
|
/// <summary>
|
/// string[] 转 ArrayList
|
/// </summary>
|
/// <param name="_Arr"></param>
|
/// <returns></returns>
|
public static ArrayList JStringsToArrayList(this string[] _Arr)
|
{
|
ArrayList List = new ArrayList();
|
|
if (_Arr == null)
|
{
|
_Arr = new string[] { };
|
}
|
|
foreach (string s in _Arr)
|
{
|
List.Add(s.ToString2());
|
}
|
return List;
|
}
|
/// <summary>
|
/// int?[] 转 ArrayList
|
/// </summary>
|
/// <param name="_Arr"></param>
|
/// <returns></returns>
|
public static ArrayList JIntsToArrayList(this int?[] _Arr)
|
{
|
ArrayList List = new ArrayList();
|
foreach (int? s in _Arr)
|
{
|
List.Add(s);
|
}
|
return List;
|
}
|
/// <summary>
|
/// string[] 转 int?[]
|
/// </summary>
|
/// <param name="_Arr"></param>
|
/// <returns></returns>
|
public static int?[] JStringsToInts(this string[] _Arr)
|
{
|
ArrayList List = new ArrayList();
|
foreach (string s in _Arr)
|
{
|
if (s.isInt())
|
List.Add(s.ToInt32());
|
}
|
return (int?[])List.ToArray(typeof(int?));
|
}
|
/// <summary>
|
/// int?[] 转 string[]
|
/// </summary>
|
/// <param name="_Arr"></param>
|
/// <param name="RemoveEmptyEntries"></param>
|
/// <returns></returns>
|
public static string[] JIntsToStrings(this int?[] _Arr, bool RemoveEmptyEntries)
|
{
|
ArrayList List = new ArrayList();
|
foreach (int? s in _Arr)
|
{
|
if (s == null && !RemoveEmptyEntries)
|
List.Add(string.Empty);
|
else
|
List.Add(s.ToString2());
|
}
|
return (string[])List.ToArray(typeof(string));
|
}
|
|
/// <summary>
|
///decimal类型转换成int强加1
|
/// </summary>
|
/// <param name="Num">源数据</param>
|
/// <returns></returns>
|
public static int JRoundInt(this decimal Num)
|
{
|
decimal f = Num;
|
int b = 0;
|
try
|
{
|
b = (int)f;
|
if ((decimal)b < f)
|
{
|
b++;
|
}
|
}
|
catch
|
{
|
b = int.MaxValue;
|
}
|
return b;
|
}
|
#endregion
|
|
#region 格式判断
|
/// <summary>
|
/// 判断参数是否为邮箱
|
/// </summary>
|
/// <param name="param"></param>
|
public static bool isEmail(this object param)
|
{
|
Regex regex = new Regex("(^[_.0-9A-Za-z-]+@[0-9A-Za-z-]+.(com|cc|cn|tv|hk|name|mobi|net|biz|org|info|gov.cn|com.cn|net.cn|org.cn)$)");
|
if (string.IsNullOrEmpty(param.ToString2()))
|
return false;
|
|
if (regex.IsMatch(param.ToString2()) && param.ToString2().Length <= 50)
|
return true;
|
else
|
return false;
|
}
|
/// <summary>
|
/// 判断参数是否为QQ号码
|
/// </summary>
|
/// <param name="param">是true </param>
|
public static bool isQQ(this object param)
|
{
|
Regex regex = new Regex("(^[^0]d{4,11}$)");
|
if (string.IsNullOrEmpty(param.ToString2()))
|
return false;
|
|
if (regex.IsMatch(param.ToString2()))
|
return true;
|
else
|
return false;
|
}
|
|
/// <summary>
|
/// 判断参数是否为手机号码
|
/// </summary>
|
/// <param name="param">是true </param>
|
public static bool isMobileNumber(this object param)
|
{
|
Regex regex = new Regex("(^[0-9]{11}$)");
|
if (string.IsNullOrEmpty(param.ToString2()))
|
return false;
|
|
if (regex.IsMatch(param.ToString2()))
|
return true;
|
else
|
return false;
|
}
|
|
#endregion
|
}
|
}
|