Skip to content

Add common helper methods to the String class (Contains(StringComparison), Replace(StringComparison), Left, Truncate, TrimPrefix) #14831

Closed
@GSPP

Description

@GSPP

The String class is missing some helper methods for string manipulation that are commonly needed. Of course it is possible for developers to add extension methods but that should not be required. here, I'll propose a list of methods that I think can be easily understood and are commonly required. The ordering of this list starts with the least controversial in my mind.

  1. bool Contains(this string str, string value, StringComparison comparisonType). We have no Contains version that takes a StringComparison. The workaround with IndexOf is awkward and feels like magic.
  2. Replace(..., StringComparison). Right now, Replace always uses Ordinal.
  3. string Left(this string str, int count) as well as Right. Left is equivalent to str.Substring(0, count) but right is str.Substring(str.Length - count, count).
  4. Truncate(int maxLength). This is not equivalent to Substring(maxLength) because Substring throws if the string is short.
  5. bool IsValidIndex(this string str, int index) tests whether the argument can be used to obtain a character from the indexer. That sometimes comes in handy. Also bool IsValidRange(this string str, int index, int count). Both can be useful for Debug.Assert assertions.
  6. string TrimPrefix(this string str, string prefix) as well as TrimPostfix. It turns out that when working with externally generated strings (ETL processes) it is very common to need to remove a prefix. For example to get a JIRA issue number as an int you do int.Parse("PROJ-2317".TrimPrefix("PROJ-")). Replace(..., "PROJ-", "") is not equivalent and semantically wrong. The TrimStart method cannot be used here. Further helper methods would be EnsurePrefix/Postfix.
  7. Some helpers to work on strings that might be null or empty. This comes up all the time.
        public static string EmptyToNull(this string str)
        {
            return string.IsNullOrEmpty(str) ? null : str;
        }
        public static string NullToEmpty(this string str)
        {
            return str ?? string.Empty;
        }
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        public static bool IsNullOrWhitespace(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }
  1. SplitLines(). Returns the string split into lines. Need to define how to deal with OS-specific line endings. Probably, this should just be str.Replace("\r", "").Split('\n') which seems to work for everything.

I can see some issues with some of these proposals. They are not perfect but I want to get the discussion going. I feel the BCL has a blind spot here. The list certainly starts out with a few no-brainers.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions