I'm using Lua in the NodeMCU environment. I saw the approach for whitespace trimming presented in this question.
The answer suggests this form:
str = string.gsub(str, "%s+", "")
I found it does not have any effect. Here is output from interactive commands (using LuaLoader).
> print("|",part1,"|")
| 172.16.0.19 |
> part1a = string.gsub(part1, "%s+", "")
> print("|",part1a,"|")
| 172.16.0.19 |
>
If I examine the strings in hex, I see that there are some tabs. Apparently %s (the whitespace pattern) does not consider tab a whitespace. Is there a setting to cause %s to match tab (0x09)?
The print function prints its arguments with tabs between them (after converting them to strings with tostring). To check this properly, use io.write (which does not add tabs between its arguments and only converts numbers to strings):
str = ' 172.16.0.19 '
stripped = string.gsub(str, '%s+', '')
io.write('|', str, '|\n')
io.write('|', stripped, '|\n')
Note that this will remove all spaces and newlines, even if they are inside the string. To remove those on either end:
str = string.gsub(str, '^%s*(.-)%s*$', '%1')
or at the ends of lines and the end of the string:
str = string.gsub(str, '[ \t]+%f[\r\n%z]', '')
Instead of print(something,somethingElse), do print(something..somethingElse). Concatenate the separate components - don’t just print a comma-separated list of the strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With