Removing characters from strings with LINQ

前端 未结 4 1693
失恋的感觉
失恋的感觉 2021-01-17 13:07

I\'m trying to brush up on my LINQ by writing some simple extension methods. Is there any better way to write such a function as below that removes a given list of character

相关标签:
4条回答
  • 2021-01-17 13:35

    You get a little performance increase when using a stringBuilder instead of the new string. Below results in:

    StringBuilder 00:00:13.9930633 new String 00:00:15.1495309

            string s = "ababababajjjaazsiajjsoajiojsioajlmmzaaokpdahgffaiojsia";
            var sw = new Stopwatch();
            sw.Start();
            var toRemove = new char[] { 'j', 'a', 'z' };
            for (int i = 0; i < 1000000; i++)
            {
                StringBuilder sb = new StringBuilder(s.Length, s.Length);
                foreach (var c in s) if (!toRemove.Contains(c)) sb.Append(c);
            }
            Console.WriteLine("StringBuilder " + sw.Elapsed);
            sw.Restart();
            for (int i = 0; i < 1000000; i++)
            {
                new string(s.Where(c => !toRemove.Contains(c)).ToArray());
            }
            Console.WriteLine("new String " + sw.Elapsed);
    
    0 讨论(0)
  • 2021-01-17 13:43

    I would prefer the first form with extension methods though simplified to

    public static string Remove(this string s, IEnumerable<char> chars)
    {
        return new string(s.Where(c => !chars.Contains(c)).ToArray());
    }
    

    As for select keyword, it's obligatory in second form. The documentation says what "A query expression must terminate with either a select clause or a group clause". That's why I would avoid LINQ syntactic sugar.

    0 讨论(0)
  • 2021-01-17 13:52

    try this for terseness

    public static string Remove(this string source, IEnumerable<char> chars) {
      return new String(source.Where(x => !chars.Contains(x)).ToArray());
    }
    

    EDIT

    Updated to correct it removing duplicates from source

    0 讨论(0)
  • 2021-01-17 13:54

    Personally I tend to use the first syntax for non relational situations. When I need to perform relational operations (join), say with Expression Trees against SQL i use the later. But, this is only because its more readable for me having used SQL for a while.

    0 讨论(0)
提交回复
热议问题