C# 使用起始字符和结束字符拆分字符串

C# 使用起始字符和结束字符拆分字符串,c#,split,C#,Split,我需要用“变量序列”拆分字符串。 例如,我有以下字符串: string myString="|1 Test 1|This my first line.|2 Test 2|This is my second line"; 我需要得到一个字符串数组: 这是我的第一句话 这是我的第二行。 同时,最好的人中最好的人会得到这个: |1测试1 | 这是我的第一句话 |2测试2 | 这是我的第二行。 有什么帮助吗 使用|作为参数,如果一个数字和一个数字加上|开始和结束,则用于子字符串。这够有用吗 st

我需要用“变量序列”拆分字符串。

例如,我有以下字符串:

string myString="|1 Test 1|This my first line.|2 Test 2|This is my second line";
我需要得到一个字符串数组:

这是我的第一句话
这是我的第二行。

同时,最好的人中最好的人会得到这个:
|1测试1 |
这是我的第一句话
|2测试2 |
这是我的第二行。

有什么帮助吗

使用|作为参数,如果一个数字和一个数字加上|开始和结束,则用于子字符串。这够有用吗

string[] myStrings = myString.Split('|');
这将为您提供一个4元素数组,其中包括:

1 Test 1
This is my first line.
2 Test 2
This is my second line.
从这里开始,我认为您将被迫遍历数组的元素,并根据当前元素的内容确定后续元素的正确操作过程

string myString=“|1测试1 |这是我的第一行。| 2测试2 |这是我的第二行”;
string myString = "|1 Test 1|This my first line.|2 Test 2|This is my second line";
            string[] mainArray = myString.Split('|');
            String str = "";
            List<string> firstList = new List<string>();
            List<string> secondList = new List<string>();
            for (int i = 1; i < mainArray.Length; i++)
            {
                if ((i % 2) == 0)
                {
                    str += "\n|" + mainArray[i];
                    firstList.Add(mainArray[i]);
                }
                else
                {
                    str += "\n|" + mainArray[i] + "|";
                    secondList.Add(mainArray[i]);
                }
            }
string[]mainArray=myString.Split(“|”); 字符串str=“”; List firstList=新列表(); List secondList=新列表(); for(int i=1;i
使用LINQ,您可以这样做:

public IEnumerable<string> GetLines(string input)
{
    foreach (var line in input.Split(new [] {'|' }, StringSplitOptions.RemoveEmptyEntries))
    {
        if (Char.IsDigit(line[0]) && Char.IsDigit(line[line.Length - 1]))
            yield return "|" + line + "|";

        yield return line;
    }
}
public IEnumerable GetLines(字符串输入)
{
foreach(input.Split(new[]{'|'},StringSplitOptions.RemoveEmptyEntries)中的var行)
{
if(Char.IsDigit(第[0]行)和&Char.IsDigit(第[line.Length-1]行)
收益率回报率“|”+行+“|”;
收益率回归线;
}
}

用“|”拆分字符串并手动添加似乎是最好的策略

        string s = "|test1|This is a string|test2|this is a string";
        string[] tokens = s.Split(new char[] { '|' });

        string x = "";

        for (int i = 0; i < tokens.Length; i++)
        {
            if (i % 2 == 0 && tokens[i].Length > 0)
            {
                x += "\n" + tokens[i] + "\n";

            }
            else if(tokens[i].Length > 0)
            {
                x += "|" + tokens[i] + "|";
            }
        }
string s=“|test1 |这是一个字符串| test2 |这是一个字符串”;
string[]tokens=s.Split(新字符[]{'|'});
字符串x=“”;
for(int i=0;i0)
{
x+=“\n”+令牌[i]+“\n”;
}
else if(标记[i]。长度>0)
{
x+=“|”+代币[i]+“|”;
}
}

您可以使用正则表达式拆分字符串,例如

string str = "|1 Test 1|This is my first line.|2 Test 2|This is my second line.";
var pattern = @"(\|(?:.*?)\|)";
foreach (var m in System.Text.RegularExpressions.Regex.Split(str, pattern))
{
    Console.WriteLine(m);
}

只需丢弃第一个条目(因为它将是空的)

这将给出6个元素,其中两个为空