C#共用工具类


using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Utilities
{
///


/// 共用工具类
///

public static class Tools
{
#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);
    }
    #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+);",
        @"-->",
        @"
							
C