Skip to main content
trim(str, cutset) removes any character in cutset from the beginning and end of str.

Signature

cutset is treated as a set of characters to remove, not a literal string to match. Every character in cutset is independently stripped.

Example

Strip unwanted characters from a version string that may have been tagged with v, V, or leading zeros:
v01.2.3 becomes 1.2.3.

Notes

  • cutset is a set of characters, not a substring. trim("hello", "helo") returns an empty string because all those characters appear at the edges.
  • To remove a specific prefix or suffix string (not a set of characters), use trimprefix or trimsuffix.
  • To remove only whitespace, use trimspace.