-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
How many times have we all written the below code?
string[] entries = someString.Split(',');
for (int i = 0; i < entries.Length; i++)
{
string entry = entries[i].Trim();
/* operate on entry */
}Even just a cursory search of our own Framework reference sources shows that this is a common pattern:
- https://referencesource.microsoft.com/#mscorlib/system/enum.cs,434
- https://referencesource.microsoft.com/#mscorlib/system/AppContext/AppContextDefaultValues.cs,122
- https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Annotations/Annotation.cs,665
- https://referencesource.microsoft.com/#System/compmod/system/codedom/compiler/CodeDomConfigurationHandler.cs,262
- https://referencesource.microsoft.com/#System/net/System/Net/_AutoWebProxyScriptHelper.cs,582
- https://referencesource.microsoft.com/#System/net/System/Net/_HeaderInfoTable.cs,84
- etc.
I propose adding an enum value StringSplitOptions.TrimEntries that would make this pattern a bit easier for folks by eliminating the need for developers to call Trim.
[Flags]
public enum StringSplitOptions
{
None = 0,
RemoveEmptyEntries = 1,
TrimEntries = 2 /* new value */
}The behavior of string.Split and related APIs would be as follows:
If None or RemoveEmpyEntries is provided, the behavior of string.Split under this proposal is unchanged from its behavior today.
If TrimEntries is provided by itself, the string.Split method performs the equivalent of calling string.Trim() on each element of the returned array. That is, no element in the returned array will have leading or trailing whitespace. The array may contain zero-length strings for entries.
Assert.Equal(new[] { "Doe", "John" }, "Doe, John".Split(',', StringSplitOptions.TrimEntries));
Assert.Equal(new[] { "1", "", "2", "3" }, "1, , 2 , 3 ".Split(',', StringSplitOptions.TrimEntries));
Assert.Equal(new[] { "", "" }, ", ".Split(',', StringSplitOptions.TrimEntries));If TrimEntries and RemoveEmptyEntries are both provided, the entries are trimmed first, and then zero-length entries are removed from the returned array.
Assert.Equal(new[] { "Doe", "John" }, "Doe, John".Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
Assert.Equal(new[] { "1", "2", "3" }, "1, , 2 , 3 ".Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
Assert.Equal(new string[0], ", ".Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));Adding this enum value may be considered a breaking change if we expect that third-party components aside from string.Split are consuming the StringSplitOptions enum, not simply generating the value. In theory any such third-party components should be checking their inputs and rejecting unknown flags.