Removing characters from strings with LINQ

前端 未结 4 1665
失恋的感觉
失恋的感觉 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:52

    try this for terseness

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

    EDIT

    Updated to correct it removing duplicates from source

提交回复
热议问题