首页 > 开发 > 综合 > 正文

C#中的字符串截取

2024-07-21 02:26:40
字体:
来源:转载
供稿:网友

///<summary>
    ///功能:精确的按要求截取指定长度的字符串
    ///中文算2个英文算一个
    ///参数:str 待截取字符,len 截取长度 symbol 表示字符(....)
    ///</summary>
    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;
    }
    /// <summary>
    /// 过滤掉html标签
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string nohtml(string str)
    {
        str = regex.replace(str, @"(/<.[^/<]*/>)", " ", regexoptions.ignorepatternwhitespace | regexoptions.ignorecase);
        str = regex.replace(str, @"(/<//[^/<]*/>)"," ", regexoptions.ignorecase | regexoptions.ignorepatternwhitespace);
        return str;
       
    }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表