Search code examples
luaneovim

Is there a way to split a string and preserve spaces in lua?


I have a table with strings like " item", " item". To render this items with indentation. I want a way to split this string and count how many spaces the string have on the beginning.


Solution

  • I something like this what you're looking for?

    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.")
    

    Output:

    The original word is [    test] and the trimmed word is [test] and there are 4 spaces.