Search code examples
stringdartapache-commons-lang

String.trimLeft and trimRight in dart


How to define trimLeft and trimRight functions for String? I can't find a simple solution to implement them.

My tricky solution is:

String _trimLeft(String str) {
  var s = (str + '.').trim();
  return s.substring(0, s.length-1);
}

String _trimRight(String str) {
  return ('.' + str).trim().substring(1);
}

Is there any 3rd-party libraries for Dart just like commons-lang to java, provides such basic functions?


Solution

  • See this answer that uses String.replace and a RegExp:

    Trimming whitespace in Dart strings from beginning or end

    I help maintain a general utility package called Quiver, but it doesn't have trim functions. They would be a great addition.

    https://github.com/google/quiver-dart