c# - 具有多个分隔符的条件分割字符串

标签 c# asp.net string delimiter

我有一个字符串

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/

相关文章:

java - 周期字符串

c# - System.AccessViolationException 从 C# 调用 c++ 函数

c# - Selenium C# Webdriver - IgnoreExceptionTypes 不起作用

asp.net - 部署时自定义 JsonConverter 被忽略

asp.net - Javascript 禁用我的回发

string - 将int值附加到字符串

c# - 如何反转包含代理项对的字符串

c# - 我的应用程序可以在我的 PC (Windows 7) 上运行,但不能在另一台 (XP) 上运行,我不确定如何继续诊断问题

c# - 枚举 Linq.Xelement

mysql - 在 where 子句 mysql 查询中使用子字符串的正确方法