主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:191次

///


    ///功能:精确的按要求截取指定长度的字符串
    ///中文算2个英文算一个
    ///参数:str 待截取字符,len 截取长度 symbol 表示字符(....)
    ///

    public static string GotTopic(string str,int len,string symbol)
    {
        int count = 0;
        string strTemp = "";
        str = NoHTML(str);
        for (int i = 0; i < str.Length; i++)
        {
            if (Math.Abs(((int)(str.Substring(i, 1).ToCharArray())[0])) > 255)
            {
                count += 2;
            }else
            {
                count += 1;
            }
            if (count <= len)
            {
                strTemp += str.Substring(i, 1);
            }
            else
            {
                return strTemp + symbol;
            }

        }
        return str;
    }
    ///


    /// 过滤掉HTML标签
    ///

    ///
    ///
    public static string NoHTML(string str)
    {
        str = Regex.Replace(str, @"(\<.[^\<]*\>)", " ", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
        str = Regex.Replace(str, @"(\<\/[^\<]*\>)"," ", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
        return str;
       
    }

关键字词: