C# 按多个字符分割

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11718653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:56:54  来源:igfitidea点击:

Split by multiple characters

c#split

提问by David Fornas

The result of doing

做的结果

var b = "asfsadefbweabgggggggggggg".Split("ab".ToCharArray());

is a list of 6 strings while I want to split the array in "asfsadefbwe"and "gggggggggggg". Is there any way/method to properly do that (with C#)?

是 6 个字符串的列表,而我想将数组拆分为"asfsadefbwe""gggggggggggg"。有什么方法/方法可以正确地做到这一点(使用 C#)?

PS: I'll use a string which has some data separate by "\r\n"secuences.

PS:我将使用一个字符串,它有一些由"\r\n"secuences分开的数据。

采纳答案by Darin Dimitrov

string[] list = b.Split(new string[] { "ab" }, StringSplitOptions.None);

回答by Joey

Use another overload, one that doesn't split on individual characters:

使用另一个重载,一个不拆分单个字符的重载:

 "asfsadefbweabgggggggggggg".Split(new [] {"ab" }, StringSplitOptions.None)

回答by tomfanning

Are your substrings always the same length? If so, use String.Substring.

你的子串总是相同的长度吗?如果是这样,请使用String.Substring