相关疑难解决方法(0)

如何按字符串拆分字符串并使用.NET包含分隔符?

有许多类似的问题,但显然没有完美的匹配,这就是我要问的原因.

我想分裂一个随机字符串(如123xx456yy789通过字符串分隔符的列表)(例如xx,yy),并包括在结果中的分隔符(在这里:123,xx,456,yy,789).

良好的表现是一个很好的奖金.如果可能的话,应该避免使用正则表达式.

更新:我做了一些性能检查并比较了结果(虽然懒得正式检查).测试的解决方案是(随机顺序):

  1. 加布
  2. Guffa
  3. 马腹
  4. 正则表达式

其他解决方案未经过测试,因为它们与其他解决方案类似,或者来得太晚.

这是测试代码:

class Program
{
    private static readonly List<Func<string, List<string>, List<string>>> Functions;
    private static readonly List<string> Sources;
    private static readonly List<List<string>> Delimiters;

    static Program ()
    {
        Functions = new List<Func<string, List<string>, List<string>>> ();
        Functions.Add ((s, l) => s.SplitIncludeDelimiters_Gabe (l).ToList ());
        Functions.Add ((s, l) => s.SplitIncludeDelimiters_Guffa (l).ToList ());
        Functions.Add ((s, l) => s.SplitIncludeDelimiters_Naive (l).ToList ());
        Functions.Add …
Run Code Online (Sandbox Code Playgroud)

.net c# string

32
推荐指数
2
解决办法
3万
查看次数

标签 统计

.net ×1

c# ×1

string ×1