C# 字符串在分隔符之间拆分

C# String Split Between Dividers

可能是我误解了标题,抱歉。 我有一个字符串;

This is a test string. It was opened on %Date% for the customer named %Customer%.

这里我希望能够用一个方法得到一个包含“Date”和“Customer”的字符串。如何将特殊字符“%”之间的部分作为字符串获取?

分隔符和字符串可以动态变化。提前致谢。

您可以使用 System.Text.RegularExpressions.RexEx()

提取它

%[^%]*%" - 以 % 开头,以 % 结尾但不包含 [^%]*

string str = "This is a test string. It was opened on %Date% for the customer named %Customer%.";
string[] res = Regex.Matches(str, "%[^%]*%").Cast<Match>().Select(x => x.Value.Trim('%')).ToArray();