我有一个字符串
string astring="#This is a Section*This is the first category*This is the
second Category# This is another Section";
我想根据分隔符分隔该字符串。如果我在开头有#,这将指示部分字符串(string[] 部分)。如果字符串以 * 开头,这将表明我有一个类别(string[] 类别)。 结果我想要
string[] section = { "This is a Section", "This is another Section" };
string[] category = { "This is the first category ",
"This is the second Category " };
我找到了这个答案: string.split - by multiple character delimiter 但这不是我想做的。
最佳答案
string astring=@"#This is a Section*This is the first category*This is the second Category# This is another Section";
string[] sections = Regex.Matches(astring, @"#([^\*#]*)").Cast<Match>()
.Select(m => m.Groups[1].Value).ToArray();
string[] categories = Regex.Matches(astring, @"\*([^\*#]*)").Cast<Match>()
.Select(m => m.Groups[1].Value).ToArray();
关于c# - 具有多个分隔符的条件分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18077125/