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

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

2020966-3cdaf55065747f7d.png
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;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_34168880/article/details/87514447