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
{
///
/// 数据类型转换类
///
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;
}
///
/// 转为Boolean
/// 2013-5-22 10:00修改实现为:true\"true"\"TRUE"\1 均为true
///
///
///
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 常用方法
///
/// object转GUID
///
///
///
public static Guid ToGuid2(this object obj)
{
Guid Val;
Val = MyConvert.ConvertToGuid(obj);
return Val;
}
///
/// object转String,如何object为null返回Empty
///
///
///
public static string ToString2(this object obj)
{
//string Val = string.Empty;
//try { Val = obj.ToString(); }
//catch { Val = ""; }
return obj == null ? string.Empty : obj.ToString();
}
///
/// object转Int,失败返回0
///
///
///
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;
}
///
/// object转Double,失败返回0
///
///
///
public static double? ToDouble2(this object obj)
{
double? Val = 0;
Val = MyConvert.ConvertToDouble(obj);
return Val;
}
///
/// object转Decimal,失败返回0
///
///
///
public static decimal? ToDecimal2(this object obj)
{
decimal? Val = 0;
Val = MyConvert.ConvertToDecimal(obj);
return Val;
}
///
/// object转Double,失败返回0
///
///
///
public static string ToDecimal2Yen(this object obj)
{
decimal? Val = obj.ToDecimal2();
if (Val.HasValue)
{
return Val.Value.ToString("0.00");
}
else
{
return "0.00";
}
}
///
/// object转DateTime
///
///
///
public static DateTime? ToDateTime2(this object obj)
{
return MyConvert.ConvertToDateTime(obj);
}
///
/// DateTime?格式化,如何DateTime?为null返回Empty
///
///
///
///
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;
}
///
/// object转Boolean,失败返回flase
///
///
///
public static bool ToBoolean2(this object obj)
{
bool Val = false;
try
{
Val = Convert.ToBoolean(obj);
}
catch
{
Val = false;
}
return Val;
}
///
/// 判断URL参数是否为正整数
///
///
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;
}
///
/// 判断参数是否为Double(比如价格)
///
/// 参数
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;
}
///
/// 检查中英文混合字符长度(英文字符算1,中文算2)
///
///
///
public static int JStrLen(this string _String)
{
return DealString.ZhuanHuanStringASIIC(_String.ToString2()).Length;
}
///
/// 截取字符串
///
/// 源字符串
/// 保留长度后面加...
///
public static string JSubString(this object Str, int length)
{
return Str.JSubString(length, "...");
}
///
/// 截取字符串
///
/// 源字符串
/// 保留长度
/// 后面的符号 例如...
///
public static string JSubString(this object Str, int length, string Sig)
{
return DealString.getSubString(Str.ToString2(), length, Sig).JHtmlEncode();
}
///
/// 去除字符串里面的HTML标签
///
/// 字符串
///
public static string JreplaceHTML(this object obj)
{
string strHtml = obj.ToString2();
return DealString.replaceHTML(strHtml);
}
///
/// 去掉字符串里面的换行符
///
/// 字符串
///
public static string JclearString_R_N(this object Str)
{
return DealString.clearString_R_N(Str.ToString2());
}
///
/// 把字符串转换成16进制
///
///
///
public static string JStrToHex(this string mStr)
{
return DealString.StrToHex(mStr);
}
///
/// 把16进制转换成字符串
///
///
///
public static string JHexToStr(this string mHex)
{
return DealString.HexToStr(mHex);
}
///
/// 对字符串进行md5加密
///
/// 字符串对象
/// 加密有的字符串
public static string Jmd5(this string _String)
{
return DealString.MD5(_String);
}
///
/// 取得此字符串的后缀名(针对字符串为文件名的情况,不包括.)
///
/// 字符串对象
/// 截取后的子字符串
public static string JgetExtension(this string _String)
{
return DealString.getExtension(_String);
}
///
/// 格式化字符串(,1,2,3,4,5,,2,32,,)为(1,2,3,4,5,2,32), 如果为空则返回字符串0
///
/// 原字符串
/// 分割标识
/// 必须为Int
///
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);
}
///
/// 将双字节字符转换成 ww
///
/// 待转换的字符串
/// 返回转换后的字符串
public static string JDoubleByteToTwoByte(this string Str)
{
Regex reg = new Regex(@"[^\x00-\xff]");
return reg.Replace(Str, "ww");
}
///
/// 将已经为HTTP传输过的HTML编码字符串转换为已解码的字符串
///
///
///
public static string JHtmlDecode(this string textToFormat)
{
if (string.IsNullOrEmpty(textToFormat))
{
return textToFormat;
}
return HttpUtility.HtmlDecode(textToFormat);
}
///
/// 将字符串转换为html编码的字符串
///
///
///
public static string JHtmlEncode(this object textToFormat)
{
string _textToFormat = textToFormat.ToString2();
if (string.IsNullOrEmpty(_textToFormat))
{
return _textToFormat;
}
return HttpUtility.HtmlEncode(_textToFormat);
}
///
/// 把带有注入性的字符串转换成普通字符
///
/// 待检查的字符串
///
public static string FilterSQL(this string _string)
{
if (_string == null)
{
return null;
}
return DealString.ChangeSQL(_string);
}
#endregion
#region ArrayList Int[] String[] String
///
/// ArrayList转字符串,每项以,分隔
///
/// 源ArrayList
///
public static string JArrayListToString(this ArrayList _ArrayList)
{
return JArrayListToString(_ArrayList, ",", false);
}
///
/// ArrayList转字符串,每项以,分隔
///
/// 源ArrayList
/// 是否去掉 空 值
///
public static string JArrayListToString(this ArrayList _ArrayList, bool RemoveEmptyEntries)
{
return JArrayListToString(_ArrayList, ",", RemoveEmptyEntries);
}
///
/// ArrayList转字符串,每项以Sig分隔
///
/// 源ArrayList
/// 分隔符
///
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);
}
///
/// Double?[] 转 Double[]
///
///
///
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));
}
///
/// Int?[] 转 Int[]
///
///
///
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));
}
///
/// string[] 转 ArrayList
///
///
///
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;
}
///
/// int?[] 转 ArrayList
///
///
///
public static ArrayList JIntsToArrayList(this int?[] _Arr)
{
ArrayList List = new ArrayList();
foreach (int? s in _Arr)
{
List.Add(s);
}
return List;
}
///
/// string[] 转 int?[]
///
///
///
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?));
}
///
/// int?[] 转 string[]
///
///
///
///
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));
}
///
///decimal类型转换成int强加1
///
/// 源数据
///
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 格式判断
///
/// 判断参数是否为邮箱
///
///
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;
}
///
/// 判断参数是否为QQ号码
///
/// 是true
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;
}
///
/// 判断参数是否为手机号码
///
/// 是true
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
}
}