asp.net字符串截取函数介绍

作者:袖梨 2022-06-25

字符截取函数

 代码如下 复制代码

///  


        ///   将指定字符串按指定长度进行剪切,
        ///  

        ///   需要截断的字符串
        ///   字符串的最大长度
        ///   超过长度的后缀
        ///   如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串
        public static string StringTruncat(string oldStr, int maxLength, string endWith)
        {
            if (string.IsNullOrEmpty(oldStr))
                //   throw   new   NullReferenceException( "原字符串不能为空 ");
                return oldStr + endWith;
            if (maxLength < 1)
                throw new Exception("返回的字符串长度必须大于[0] ");
            if (oldStr.Length > maxLength)
            {
                string strTmp = oldStr.Substring(0, maxLength);
                if (string.IsNullOrEmpty(endWith))
                    return strTmp;
                else
                    return strTmp + endWith;
            }
            return oldStr;
        }

调用方法

 代码如下 复制代码

<%# StringTruncat(Eval("Title").ToString(), 12, "...")%>

例2

 代码如下 复制代码

public string GetContent(string str, string start, string last, int n)
    {
        if (str.ToLower().IndexOf(start.ToLower()) >= 0)
        {
            if (str.ToLower().IndexOf(last.ToLower()) >= 0)
            {
                switch (n)
                {
                    //左右都截取(都取前面)(包含关键字)
                    case 1: str = str.Substring(str.ToLower().IndexOf(start.ToLower()), str.Length - str.ToLower().IndexOf(start.ToLower())); str = str.Substring(0, str.ToLower().IndexOf(last.ToLower()) + last.Length); break;
                    //左右都截取(都取前面)(去除关键字)
                    case 2: str = str.Substring(str.ToLower().IndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().IndexOf(start.ToLower()) - start.Length); str = str.Substring(0, str.ToLower().IndexOf(last.ToLower())); break;
                    //左右都截取(都取后面)(包含关键字)
                    case 3: str = str.Substring(str.ToLower().LastIndexOf(start.ToLower()), str.Length - str.ToLower().LastIndexOf(start.ToLower())); str = str.Substring(0, str.ToLower().LastIndexOf(last.ToLower()) + last.Length); break;
                    //左右都截取(都取后面)(去除关键字)
                    case 4: str = str.Substring(str.ToLower().LastIndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().LastIndexOf(start.ToLower()) - start.Length); str = str.Substring(0, str.ToLower().LastIndexOf(last.ToLower())); break;
                    //左右都截取(一前一后)(包含关键字)
                    case 5: str = str.Substring(str.ToLower().IndexOf(start.ToLower()), str.Length - str.ToLower().IndexOf(start.ToLower())); str = str.Substring(0, str.ToLower().LastIndexOf(last.ToLower()) + last.Length); break;
                    //左右都截取(一前一后)(去除关键字)
                    case 6: str = str.Substring(str.ToLower().IndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().IndexOf(start.ToLower()) - start.Length); str = str.Substring(0, str.ToLower().LastIndexOf(last.ToLower())); break;
                    default: str = ""; break;
                }
            }
            else
            {
                switch (n)
                {
                    //只往左截取(取前面的)(包含关键字)
                    case 7: str = str.Substring(0, str.ToLower().IndexOf(start.ToLower()) + start.Length); break;
                    //只往左截取(取前面的)(去除关键字)
                    case 8: str = str.Substring(0, str.ToLower().IndexOf(start.ToLower())); break;
                    //只往左截取(取后面的)(包含关键字)
                    case 9: str = str.Substring(0, str.ToLower().LastIndexOf(start.ToLower()) + start.Length); break;
                    //只往左截取(取后面的)(去除关键字)
                    case 10: str = str.Substring(0, str.ToLower().LastIndexOf(start.ToLower())); break;
                    //只往右截取(取前面的)(包含关键字)
                    case 11: str = str.Substring(str.ToLower().IndexOf(start.ToLower()), str.Length - str.ToLower().IndexOf(start.ToLower())); break;
                    //只往右截取(取前面的)(去除关键字)
                    case 12: str = str.Substring(str.ToLower().IndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().IndexOf(start.ToLower()) - start.Length); break;
                    //只往右截取(取后面的)(包含关键字)
                    case 13: str = str.Substring(str.ToLower().LastIndexOf(start.ToLower()), str.Length - str.ToLower().LastIndexOf(start.ToLower())); break;
                    //只往右截取(取后面的)(去除关键字)
                    case 14: str = str.Substring(str.ToLower().LastIndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().LastIndexOf(start.ToLower()) - start.Length); break;
                    default: str = "无"; break;
                }
            }
        }
        else
        {
            str = "无!";
        }
        return str

}

相关文章

精彩推荐