How to split the string in C#?

tes*_*zoo 0 c#

Note:

string s="Error=0<BR>Message_Id=120830406<BR>"
Run Code Online (Sandbox Code Playgroud)

What's the most elegant way to split a string in C#?

Dan*_*Tao 5

假设您要拆分<BR>元素:

string[] lines = s.Split(new[] { "<BR>" }, StringSplitOptions.None);
Run Code Online (Sandbox Code Playgroud)

请注意,这将会带出<BR>元素本身。如果您想包含这些,您可以使用Regex该类或编写自己的方法来执行此操作(最有可能使用string.Substring)。

我的建议通常是谨慎使用正则表达式,实际上,因为它们最终会变得相当难以理解。也就是说,在这种情况下,您可以如何使用它们:

string[] lines = Regex.Matches(s, ".*?<BR>")
    .Cast<Match>()
    .Select(m => m.Value)
    .ToArray();
Run Code Online (Sandbox Code Playgroud)