获取字符串中指定字符间的字符串

 /// <summary>
    /// 获取字符串中指定字符间的字符串
    /// </summary>
    /// <param name="str">要截取的字符串</param>
    /// <param name="start">开始字符串</param>
    /// <param name="last">结束字符串</param>
    /// <param name="n">类型</param>
    /// <returns></returns>
    public string GetContent(string str, string start, string last, int n)
    {
        switch (n)
        {
            //左右都截取(都取前面)(包含关键字)
            case 1:
                str = str.Substring(str.IndexOf(start), str.Length - str.IndexOf(start));
                str = str.Substring(0, str.IndexOf(last) + last.Length);
                break;
            //左右都截取(都取前面)(去除关键字)
            case 2:
                str = str.Substring(str.IndexOf(start) + start.Length, str.Length - str.IndexOf(start) - start.Length);
                str = str.Substring(0, str.IndexOf(last));
                break;
            //左右都截取(都取后面)(包含关键字)
            case 3:
                str = str.Substring(str.LastIndexOf(start), str.Length - str.LastIndexOf(start));
                str = str.Substring(0, str.LastIndexOf(last) + last.Length);
                break;
            //左右都截取(都取后面)(去除关键字) 
            case 4:
                str = str.Substring(str.LastIndexOf(start) + start.Length, str.Length - str.LastIndexOf(start) - start.Length);
                str = str.Substring(0, str.LastIndexOf(last));
                break;
            //左右都截取(一前一后)(包含关键字)
            case 5:
                str = str.Substring(str.IndexOf(start), str.Length - str.IndexOf(start));
                str = str.Substring(0, str.LastIndexOf(last) + last.Length);
                break;
            //左右都截取(一前一后)(去除关键字) 
            case 6:
                str = str.Substring(str.IndexOf(start) + start.Length, str.Length - str.IndexOf(start) - start.Length);
                str = str.Substring(0, str.LastIndexOf(last));
                break;
            default:
                str = "";
                break;
        }
        return str;
    }

  

posted @ 2012-01-11 15:31  红鲤鱼与驴与绿鲤鱼  阅读(1257)  评论(0编辑  收藏  举报