C# Extension Method - String Split that also accepts an Escape Character

后端 未结 10 1720
我在风中等你
我在风中等你 2020-12-17 00:19

I\'d like to write an extension method for the .NET String class. I\'d like it to be a special varation on the Split method - one that takes an escape character to prevent s

10条回答
  •  长情又很酷
    2020-12-17 00:33

    My first observation is that the separator ought to be a char not a string since escaping a string using a single character may be hard -- how much of the following string does the escape character cover? Other than that, @James Curran's answer is pretty much how I would handle it - though, as he says it needs some clean up. Initializing j to 0 in the loop initializer, for instance. Figuring out how to handle null inputs, etc.

    You probably want to also support StringSplitOptions and specify whether empty string should be returned in the collection.

提交回复
热议问题