# [NTG-context] Trimming strings and Lua string.trim question

Rik Kabel context at rik.users.panix.com
Wed Mar 5 05:45:39 CET 2014

I am trying to strip all leading and trailing whitespace from a string.
I have tried two methods, neither of which is satisfactory.

The first method uses \ignorespaces and \removeunwantedspaces. This
fails to strip leading and trailing newlines.

The second method uses the Lua string.strip function. The wiki description:

Yields string with leading and trailing whitespace (spaces,
horizontal and vertical tabs, newlines) removed

suggests that this should do what I want, but I find that it removes
internal whitespace as well as the leading and trailing whitespace.

Can somebody suggest a better way of doing this?

The following code demonstrates the problem:

% macros=mkvi
\starttexdefinition StringStrip #STRING
\startluacode
context(string.strip([==[#STRING]==]))
\stopluacode
\stoptexdefinition
\long\def\test{

And it has an unexpected result.

With \tex{ignorespaces} and \tex{removeunwantedspaces}, the
newlines remain.

With Lua {\tt string.strip}, the \tex{quad} at the end of a
paragraph is preserved,    but the other internal whitespace
(including newlines) is gone!

}
\starttext
\subject{ignorespaces and removeunwantedspaces}
¦\ignorespaces\test\removeunwantedspaces¦

\subject{Lua string.strip}
¦\StringStrip{\test}¦
\stoptext

--
Rik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.ntg.nl/pipermail/ntg-context/attachments/20140304/c93e9ff3/attachment.html>