zoukankan      html  css  js  c++  java
  • 获取字符串中指定字符间的字符串

     /// <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;
        }
    

      

  • 相关阅读:
    2019 ICPC Universidad Nacional de Colombia Programming Contest
    AcWing
    模板
    AcWing
    AcWing
    AcWing
    模板
    模板
    自考新教材-p340
    自考新教材-p339
  • 原文地址:https://www.cnblogs.com/yhdkzy/p/2319555.html
Copyright © 2011-2022 走看看