textutil::trim(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– textutil::trim β€” bestie edition πŸ’–

okay so you know how you have a string and it's got all this extra whitespace hanging off it like glitter after a craft project? yeah. that's this. it lives in textutil, which is the text and string utilities + macro processing corner of Tcllib, and it's about trimming strings. that's the whole job. and she does it beautifully. ✨


🌸 NAME 🌸

textutil::trim β€” procedures to trim strings πŸŽ€


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require textutil::trim ?0.8?

::textutil::trim::trim string ?regexp?

::textutil::trim::trimleft string ?regexp?

::textutil::trim::trimright string ?regexp?

::textutil::trim::trimPrefix string prefix

::textutil::trim::trimEmptyHeading string

πŸ’… DESCRIPTION πŸ’…

so the textutil::trim package hands you a lil set of commands that trim strings, and the slay part is that they trim using arbitrary regular expressions. not just spaces. whatever you want. iconic behavior honestly.

the full roster of procedures is right below, no skips, no cuts. πŸ‘‘

  • ::textutil::trim::trim string ?regexp? β€” takes string, yeets any leading and trailing substring that matches the regular expression regexp, and gives you the glow-up back as a new string. and this happens on all the lines in the string, meaning every substring that sits between 2 newline chars, or between the beginning of the string and a newline, or between a newline and the end of the string β€” or, if the string has no newline at all, then between the beginning and the end of the string. regexp defaults to "[ \t]+". 🧚
  • ::textutil::trim::trimleft string ?regexp? β€” same vibe but only the leading side. removes any leading substring in string that matches regexp and returns the result as a new string. this applies to any line in the string β€” any substring between 2 newline chars, or between the beginning of the string and a newline, or between a newline and the end of the string, or, if the string has no newline, between the beginning and the end of the string. regexp defaults to "[ \t]+". πŸ¦‹
  • ::textutil::trim::trimright string ?regexp? β€” and this one is the trailing side, the lil cleanup at the end of the sentence. removes any trailing substring in string matching regexp and returns the result as a new string. applies to any line in the string β€” any substring between 2 newline chars, or between the beginning of the string and a newline, or between a newline and the end of the string, or, if the string has no newline, between the beginning and the end of the string. regexp defaults to "[ \t]+". πŸ’œ
  • ::textutil::trim::trimPrefix string prefix β€” takes the prefix off the beginning of string and hands you the result. and listen: if string doesn't actually start with prefix, she leaves it completely unchanged. no drama, no error, just vibes. 🫢
  • ::textutil::trim::trimEmptyHeading string β€” hunts down empty lines at the beginning of string and removes them. and yes, "empty" includes lines that are only whitespace, we're not fooled by a line full of spaces bestie 😭. the modified string comes back as the result of the command. πŸ“

😭 BUGS, IDEAS, FEEDBACK 😭

ngl, this document and the package it describes will undoubtedly contain bugs and other problems. that's just how it is. so please report them in the category textutil of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. and while you're there, also drop any ideas for enhancements you might have for the package and/or the documentation. we love ideas. we eat ideas for breakfast. πŸ“

when you're proposing code changes, please send unified diffs, i.e. the output of diff -u. this is not optional, it's the vibe check.

also note: attachments are strongly preferred over inlined patches. to attach something, go to the Edit form of the ticket right after you create it, then use the left-most button in the secondary navigation bar. there you go, you're welcome. πŸ’…


πŸ‘€ SEE ALSO πŸ‘€

regexp(n), split(n), string(n)


πŸ”‘ KEYWORDS πŸ”‘

prefix, regular expression, string, trimming


🌈 CATEGORY 🌈

Text processing


okay bye bestie, this was tcllib version 0.8, dated textutil::trim(n), and honestly? she ate. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from