【问题标题】:C# String Split Between DividersC# 字符串在分隔符之间拆分
【发布时间】:2021-11-15 10:10:34
【问题描述】:

也许我误解了标题,对不起。 我有一个字符串;

这是一个测试字符串。它于 %Date% 为名为的客户打开 %客户%。

这里我希望能够通过方法获取包含“日期”和“客户”的字符串。如何将特殊字符“%”之间的部分作为字符串获取?

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

【问题讨论】:

    标签: c# string split


    【解决方案1】:

    你可以用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(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-22
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 2018-05-05
      相关资源
      最近更新 更多