有没有办法在lua中分割字符串并保留空格?

问题描述 投票:0回答:1

我有一张桌子,里面有像

" item", "   item"
这样的字符串。用缩进呈现此项目。我想要一种方法来分割这个字符串并计算字符串开头有多少个空格。

lua neovim
1个回答
0
投票

我就是你要找的东西?

local original = "    test"
local trimmed = original:match( "^%s*(.*)" )
local spaces = #original - #trimmed
print("The original word is [" .. original .. "] and the trimmed word is [" .. trimmed .. "] and there are " .. spaces .. " spaces.")

输出:

The original word is [    test] and the trimmed word is [test] and there are 4 spaces.
© www.soinside.com 2019 - 2024. All rights reserved.