Skip to content

[API Proposal]: Add StringSplitOptions.RemoveDuplicates. #56761

Closed

Description

@AraHaan

Background and motivation

StringSplitOptions is missing an option to remove duplicate strings from the resulting array leaving each string their own unique item when the user does not want duplicate entries of the same string within the resulting array from calls to string.Split(...).

API Proposal

namespace System
{
  [Flags]
  public enum StringSplitOptions
  {
    None = 0,
    RemoveEmptyEntries = 1,
    TrimEntries = 2,
+    RemoveDuplicates = 4,
  }
}

API Usage

A valid usage of this is when you are making a source generator and you want to filter out the obtained string from an AdditionalFile that the C# compiler loaded up for you by each line like so:

var lines = contents?.Split(new []{ "\r\n", "\n", "\r"}, StringSplitOptions.RemoveEmptyEntries);

But then you also want Split(...) to not include duplicate lines from said AdditionalFile if they contain such but yet Split(...) does not do such a thing (yet) making you have to try to probe for duplicate entries in the array then remove and resize said array after the fact of it's creation.

After that is done then you can process those lines to generate code for the user and then everything would compile fine on their end.

Risks

I think the risk for this is minimal and the functionality should be trivial to add (hopefully).

Activity

  1. added
    api-suggestion
    on Aug 3, 2021
  2. ghost added
    untriaged
    on Aug 3, 2021
  3. WeihanLi commented on Aug 3, 2021

    @WeihanLi
    Contributor

    RemoveDuplicates = 4 is more reasonable for a flagged enum

  4. AraHaan commented on Aug 3, 2021

    @AraHaan
    MemberAuthor

    Edited, thanks.

  5. Joe4evr commented on Aug 3, 2021

    @Joe4evr
    Contributor

    Why not simply pipe the resulting array into a HashSet<string>? That way you can also pass in the kind of comparison you want to do (which is heavily recommended to do any time string operations are involved).

  6. AraHaan commented on Aug 3, 2021

    @AraHaan
    MemberAuthor

    But isn't HashSet<string> slower than using Distinct() from System.Linq;?

  7. svick commented on Aug 3, 2021

    @svick
    Member

    @AraHaan If it is, then use Distinct(). The point is that there is no reason to embed this functionality directly into string.Split, when the BCL already lets you easily and efficiently do it today.

  8. GrabYourPitchforks commented on Aug 3, 2021

    @GrabYourPitchforks
    Member

    Another benefit to piping this to a HashSet<string> is that you can specify which comparer you want to use. For example, you can specify whether you want duplicate checking done in a case-sensitive or a case-insensitive fashion.

  9. removed
    untriaged
    on Aug 9, 2021
  10. added this to the Future milestone on Aug 9, 2021
  11. added
    needs-further-triage
    on Aug 9, 2021
  12. stephentoub commented on Mar 14, 2022

    @stephentoub
    Member

    I agree with the feedback: if the consumer needs duplicates removed, they should remove the duplicates using HashSet<T> or Distinct() (which just uses HashSet<T> today). Thanks for the suggestion.

  13. ghost locked as resolved and limited conversation to collaborators on Apr 13, 2022
  14. removed
    needs-further-triage
    on Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions