using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
namespace Ant.Service.Common
{
///
/// 功能描述:共用工具类
///
public static class Tools
{
#region 删除最后结尾的指定字符后的字符
///
/// 删除最后结尾的指定字符后的字符
///
public static string DelLastChar(string str, string strchar)
{
if (string.IsNullOrEmpty(str))
return "";
if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
return str;
}
#endregion
///
/// 截取出身年月日
///
/// 身份证号
///
public static string GetDate(string IDCard)
{
string BirthDay = " ";
string strYear;
string strMonth;
string strDay;
if (IDCard.Length == 15)
{
strYear = IDCard.Substring(6, 2);
strMonth = IDCard.Substring(8, 2);
strDay = IDCard.Substring(10, 2);
BirthDay = "19" + strYear + "- " + strMonth + "- " + strDay;
}
if (IDCard.Length == 18)
{
strYear = IDCard.Substring(6, 4);
strMonth = IDCard.Substring(10, 2);
strDay = IDCard.Substring(12, 2);
BirthDay = strYear + "- " + strMonth + "- " + strDay;
}
return BirthDay;
}
#region 获得用户IP
///
/// 获得用户IP
///
public static string GetUserIp()
{
string ip;
string[] temp;
bool isErr = false;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
else
ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
if (ip.Length > 15)
isErr = true;
else
{
temp = ip.Split('.');
if (temp.Length == 4)
{
for (int i = 0; i < temp.Length; i++)
{
if (temp[i].Length > 3) isErr = true;
}
}
else
isErr = true;
}
if (isErr)
return "1.1.1.1";
else
return ip;
}
#endregion
#region 根据配置对指定字符串进行 MD5 加密
///
/// 根据配置对指定字符串进行 MD5 加密
///
///
///
public static string GetMD5(string s)
{
//md5加密
s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString();
return s.ToLower().Substring(8, 16);
}
#endregion
#region 得到字符串长度,一个汉字长度为2
///
/// 得到字符串长度,一个汉字长度为2
///
/// 参数字符串
///
public static int StrLength(string inputString)
{
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
int tempLen = 0;
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
tempLen += 2;
else
tempLen += 1;
}
return tempLen;
}
#endregion
#region 截取指定长度字符串
///
/// 截取指定长度字符串
///
/// 要处理的字符串
/// 指定长度
/// 返回处理后的字符串
public static string ClipString(string inputString, int len)
{
bool isShowFix = false;
if (len % 2 == 1)
{
isShowFix = true;
len--;
}
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
tempLen += 2;
else
tempLen += 1;
try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
}
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (isShowFix && mybyte.Length > len)
tempString += "…";
return tempString;
}
#endregion
#region 获得两个日期的间隔
///
/// 获得两个日期的间隔
///
/// 日期一。
/// 日期二。
/// 日期间隔TimeSpan。
public static TimeSpan DateDiff(DateTime DateTime1, DateTime DateTime2)
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
return ts;
}
#endregion
#region 格式化日期时间
///
/// 格式化日期时间
///
/// 日期时间
/// 显示模式
/// 0-9种模式的日期
public static string FormatDate(DateTime dateTime1, string dateMode)
{
switch (dateMode)
{
case "0":
return dateTime1.ToString("yyyy-MM-dd");
case "1":
return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
case "2":
return dateTime1.ToString("yyyy/MM/dd");
case "3":
return dateTime1.ToString("yyyy年MM月dd日");
case "4":
return dateTime1.ToString("MM-dd");
case "5":
return dateTime1.ToString("MM/dd");
case "6":
return dateTime1.ToString("MM月dd日");
case "7":
return dateTime1.ToString("yyyy-MM");
case "8":
return dateTime1.ToString("yyyy/MM");
case "9":
return dateTime1.ToString("yyyy年MM月");
default:
return dateTime1.ToString();
}
}
#endregion
#region 得到随机日期
///
/// 得到随机日期
///
/// 起始日期
/// 结束日期
/// 间隔日期之间的 随机日期
public static DateTime GetRandomTime(DateTime time1, DateTime time2)
{
Random random = new Random();
DateTime minTime = new DateTime();
DateTime maxTime = new DateTime();
System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
// 获取两个时间相隔的秒数
double dTotalSecontds = ts.TotalSeconds;
int iTotalSecontds = 0;
if (dTotalSecontds > System.Int32.MaxValue)
{
iTotalSecontds = System.Int32.MaxValue;
}
else if (dTotalSecontds < System.Int32.MinValue)
{
iTotalSecontds = System.Int32.MinValue;
}
else
{
iTotalSecontds = (int)dTotalSecontds;
}
if (iTotalSecontds > 0)
{
minTime = time2;
maxTime = time1;
}
else if (iTotalSecontds < 0)
{
minTime = time1;
maxTime = time2;
}
else
{
return time1;
}
int maxValue = iTotalSecontds;
if (iTotalSecontds <= System.Int32.MinValue)
maxValue = System.Int32.MinValue + 1;
int i = random.Next(System.Math.Abs(maxValue));
return minTime.AddSeconds(i);
}
///
/// 获取时间戳
///
public static string GetRandomTimeSpan()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
#endregion
#region HTML转行成TEXT
///
/// HTML转行成TEXT
///
///
///
public static string HtmlToTxt(string strHtml)
{
string[] aryReg ={
@"",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"(\d+);",
@"-->",
@"