一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

自定义的asp.net截取字符串代码

时间:2011-05-16 编辑:简简单单 来源:一聚教程网

自定义的asp教程.net截取字符串代码

/// 截取字符串并限制字符串长度,多于给定的长度+。。。
    ///
    /// 待截取的字符串
    /// 每行的长度,多于这个长度自动换行
    /// 输出字符串最大的长度
    ///
    public string CutStr(string str, int len, int max)
    {
        string s = "";
        string sheng = "";
        if (str.Length > max)
        {
            str = str.Substring(0, max);
            sheng = "";
        }
        for (int i = 0; i < str.Length; i++)
        {
            int r = i % len;
            int last = (str.Length / len) * len;
            if (i != 0 && i <= last)
            {

                if (r == 0)
                {
                    s += str.Substring(i - len, len) + "
";
                }

            }
            else if (i > last)
            {
                s += str.Substring(i - 1);
                break;
            }

        }

        return s + sheng;

    }

热门栏目