美文网首页unity3D技术分享
c#截取两个指定字符串中间的字符串列表

c#截取两个指定字符串中间的字符串列表

作者: 好怕怕 | 来源:发表于2019-01-18 10:05 被阅读13次

常见的很多都是截取一个字符串中的一组,其实很多时候我们需要用到截取整个字符串中,所有匹配到的字符串,那得到将是一个列表。
列如:"{{localization:0-35}u}{localization:50-50},jdjsi{emoj,{localization:12-58}}"
截取中间的坐标,根据"{localization:""}"进行匹配,得到结果如下打印

image.png
public class Test : MonoBehaviour
{

    public string mContent = "{{localization:0-35}u}{localization:50-50},jdjsi{emoj,{localization:12-58}}";
    public string mStartStr = "{localization:";
    public string mEndStr = "}";


    void Awake()
    {
        var list = GetAllSubstring(mContent, mStartStr, mEndStr);
        for (int i = 0; i < list.Count; i++)
        {
            Debug.LogError(list[i]);
        }
    }

    public List<string> GetAllSubstring(string content, string startStr, string endStr)
    {
        List<string> resultList = new List<string>();

        int len = content.Length;
        int startLen = startStr.Length;
        for (var i = 0; i < len; i++)
        {
            string a = startStr.Substring(0, 1);
            if (content[i].ToString() == a)
            {
                int startIndex = (i + startLen - 1);
                if (startIndex < len)
                {
                    a = content.Substring(i, startLen);
                    if (a.Equals(startStr))
                    {
                        // 循环找出结尾匹配
                        for (int endIndex = startIndex; endIndex < len; endIndex++)
                        {
                            if (content[endIndex].ToString() == endStr)
                            {
                                // 得到长度
                                int splLen = endIndex - startIndex;
                                string result = content.Substring(startIndex + 1, splLen - 1);
                                resultList.Add(result);
                                break;
                            }
                        }
                    }
                }
            }
        }
        return resultList;
    }
}

相关文章

  • 2019-05-24

    例如,要截取一个字符串中,两个指定字符串中间的字符串,OC截取方法如下: // 要截取 "> 和

  • c#截取两个指定字符串中间的字符串列表

    常见的很多都是截取一个字符串中的一组,其实很多时候我们需要用到截取整个字符串中,所有匹配到的字符串,那得到将是一个...

  • iOS字符串截取、包含、替换

    1.截取两个指定字符串中的字符串 NSString *string = @"abavavasdsvx,as.dsf...

  • OC 字符串的截取

    1、字符串截取 2、从指定位置开始截取n个长度 3、分隔字符串 4、字符串的截取替换 5、替换掉截取某部分的字符串...

  • 常用shell

    AWK 多个分隔符 删除文件中包含指定字符串的行 替换指定字符串 截取n到m列 diff 文件差异 字符串截取 $...

  • Lua string.sub()

    前言# 今天来看一个字符串截取函数,作用就是从原字符串中指定两个索引,截取索引之间的字符作为结果字符串,要注意这个...

  • SQL Server 2016 函数:SUBSTRING

    SUBSTRING 函数用于截取指定长度的字符串,并将截取的字符串返回 SUBSRTING 与 STUFF 对比同...

  • ios 开发 截取2个指定字符串之间的字符

    iOS 截取字符串中2个指定字符串之间的字符串 NSString *resourceString = @"=需要...

  • 字符串(NSString)拼接、截取等

    一、NSString的截取 1.从字符串开头开始截取到指定位置(不包含该位置) 2.从字符串指定位置开始截取到最后...

  • 【python】一些好用的方法(持续更新)

    将列表组成字符串 声明列表并插入数据(类似C中的数组) 将字符串按分成一定数量的列表 截取字符串 删除列表中的空值...

网友评论

    本文标题:c#截取两个指定字符串中间的字符串列表

    本文链接:https://www.haomeiwen.com/subject/shvgdqtx.html