【C#】截取一个字符串中全部的两个指定字符串中间的字符串

1.返回结果为列表<满足条件的字符串>.

		/// <summary>
        /// 截取两个指定字符串中间的字符串。
        /// </summary>
        /// <param name="outs">输出,返回列表</param>
        /// <param name="content">待处理的字符串</param>
        /// <param name="startStr">判断字符串首</param>
        /// <param name="endStr">判断字符串尾</param>
        /// <returns></returns>
        private bool GetAllSubstring_RList(out List<string>? outs, string content, string startStr, string endStr)
        {
            if (startStr == "" || endStr == "") { outs = null; return false; }
            int startLen = startStr.Length;//判断字符串首的长度
            int endLen = endStr.Length;//判断字符串尾的长度
            List<string> outList = new List<string>();//待返回的结果列表
            int BNo = -1, ENo = -1, Len, SubB;
            string ReremainderStr = content;//余下的字符串
            string SubStr;
            while (true)
            {
                Len = ReremainderStr.Length;//余下的待处理的字符串的长度
                BNo = ReremainderStr.IndexOf(startStr);
                if (BNo > -1) //寻找到了起始字符串
                {
                    SubB = BNo + startLen;
                    //截取满足条件之后的全部剩余字符串作为剩余待处理字符串
                    ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
                    Len = ReremainderStr.Length;//更新长度
                    ENo = ReremainderStr.IndexOf(endStr);//寻找结尾字符串
                    if (ENo > -1)//寻找到了结束字符串
                    {
                        SubStr = ReremainderStr.Substring(0, ENo);//截取中间字符串
                        outList.Add(SubStr);//添加进入结果列表
                        SubB = ENo + endLen;
                        //截取满足条件之后的全部剩余字符串作为剩余待处理字符串
                        ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
                    }
                    else break;
                }
                else break;
            }
            if (outList.Count == 0) { outs = null; return false; }
            outs = outList;
            return true;
        }

2.返回结果为字典<满足条件的字符串在输入字符串的首个位置,满足条件的字符串>.

		/// <summary>
        /// 截取两个指定字符串中间的字符串。
        /// </summary>
        /// <param name="outs">输出,返回字典</param>
        /// <param name="content">待处理的字符串</param>
        /// <param name="startStr">判断字符串首</param>
        /// <param name="endStr">判断字符串尾</param>
        /// <returns></returns>
        private bool GetAllSubstring_RDic(out Dictionary<int, string>? outs,string content, string startStr, string endStr)
        {
            if (startStr == "" || endStr == "") { outs = null;return false; }
            int startLen = startStr.Length;
            int endLen = endStr.Length;
            Dictionary<int,string> outList = new Dictionary<int, string>();
            int BNo = -1, ENo = -1, Len, SubB;
            int HeadLen = 0;//已舍弃的头部长度
            string ReremainderStr = content;
            string SubStr;
            while (true)
            {
                Len = ReremainderStr.Length;
                BNo = ReremainderStr.IndexOf(startStr);
                if (BNo > -1) //寻找到了起始字符串
                {
                    SubB = BNo + startLen;
                    HeadLen += SubB;
                    //截取满足条件之后的全部剩余字符串作为剩余待处理字符串
                    ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
                    Len = ReremainderStr.Length;//更新长度
                    ENo = ReremainderStr.IndexOf(endStr);//寻找结尾字符串
                    if (ENo > -1)//寻找到了结束字符串
                    {
                        SubStr = ReremainderStr.Substring(0, ENo);//截取中间字符串
                        outList.TryAdd(HeadLen, SubStr);//添加进入结果列表
                        SubB = ENo + endLen;
                        HeadLen += SubB;
                        //截取满足条件之后的全部剩余字符串作为剩余待处理字符串
                        ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
                    }
                    else break;
                }
                else break;
            }
            if (outList.Count == 0) { outs = null; return false; }
            outs = outList;
            return true;
        }

3.返回结果为字典<满足条件的字符串,该字符串出现的次数>.

		/// <summary>
        /// 截取两个指定字符串中间的字符串。
        /// </summary>
        /// <param name="outs">输出,返回字典</param>
        /// <param name="content">待处理的字符串</param>
        /// <param name="startStr">判断字符串首</param>
        /// <param name="endStr">判断字符串尾</param>
        /// <returns></returns>
        private bool GetAllSubstring_RDic(out Dictionary<string, int>? outs, string content, string startStr, string endStr)
        {
            if (startStr == "" || endStr == "") { outs = null; return false; }
            int startLen = startStr.Length;
            int endLen = endStr.Length;
            Dictionary<string, int> outList = new Dictionary<string, int>();
            int BNo = -1, ENo = -1, Len, SubB;
            string ReremainderStr = content;
            string SubStr;
            while (true)
            {
                Len = ReremainderStr.Length;
                BNo = ReremainderStr.IndexOf(startStr);
                if (BNo > -1) //寻找到了起始字符串
                {
                    SubB = BNo + startLen;
                    //截取满足条件之后的全部剩余字符串作为剩余待处理字符串
                    ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
                    Len = ReremainderStr.Length;//更新长度
                    ENo = ReremainderStr.IndexOf(endStr);//寻找结尾字符串
                    if (ENo > -1)//寻找到了结束字符串
                    {
                        SubStr = ReremainderStr.Substring(0, ENo);//截取中间字符串
                        if(outList.ContainsKey(SubStr))
                        {
                            outList[SubStr] += 1;
                        }
                        else outList.TryAdd(SubStr, 1);//添加进入结果列表
                        SubB = ENo + endLen;
                        //截取满足条件之后的全部剩余字符串作为剩余待处理字符串
                        ReremainderStr = ReremainderStr.Substring(SubB, Len - SubB);
                    }
                    else break;
                }
                else break;
            }
            if (outList.Count == 0) { outs = null; return false; }
            outs = outList;
            return true;
        }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值