小编Ana*_*ass的帖子

提取两个字符串之间的所有字符串

我正在尝试开发一个匹配两个字符串之间所有字符串的方法:

我试过这个,但它只返回第一场比赛:

string ExtractString(string s, string start,string end)
        {
            // You should check for errors in real-world code, omitted for brevity

            int startIndex = s.IndexOf(start) + start.Length;
            int endIndex = s.IndexOf(end, startIndex);
            return s.Substring(startIndex, endIndex - startIndex);
        }
Run Code Online (Sandbox Code Playgroud)

我们假设我们有这个字符串

String Text = "A1FIRSTSTRINGA2A1SECONDSTRINGA2akslakhflkshdflhksdfA1THIRDSTRINGA2"
Run Code Online (Sandbox Code Playgroud)

我想ac#function执行以下操作:

public List<string> ExtractFromString(String Text,String Start, String End)
{
    List<string> Matched = new List<string>();
    .
    .
    .
    return Matched; 
}
// Example of use 

ExtractFromString("A1FIRSTSTRINGA2A1SECONDSTRINGA2akslakhflkshdflhksdfA1THIRDSTRINGA2","A1","A2")

    // Will return :
    // FIRSTSTRING
    // SECONDSTRING
    // THIRDSTRING
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助 !

c#

11
推荐指数
2
解决办法
3万
查看次数

标签 统计

c# ×1