1

我想做一个从字符串中换行的函数。

例如:“1.” -> “\n1.”

所以我可以写这样的代码

string Input = "1. First option";
Input += "2. Second option";
Input += "3. Third option";

Output = WriteMenu(Input);

并得到一个这样的字符串

"1. First option
\n2. Second option
\n3. Third option"

模式将始终为 [number][dot][whitespace]。如果第一个选项带有新行,这不是问题。

4

3 回答 3

4

给这个人一个机会

Input = Regex.Replace(Input, @"(?<!^)(\d+\.)", "\n$1")
于 2012-04-04T00:12:02.877 回答
2
Regex rgx = new Regex("(\\d+\\.\\s)");
String replaced = rgx.Replace(Input, Environment.NewLine + "$1");
于 2012-04-04T00:19:34.000 回答
1

像这样更短的表达式也可以:

Regex.Replace(Input, @"(?!^)\d+\.", "\n$0")
于 2012-04-04T00:33:18.413 回答