Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would you remove trailing and leading whitespace?

Asked by
Kami_Yo 72
5 years ago
Edited 5 years ago

Right now this takes a bunch of words separated by a comma and spits them up by the comma. My only issue is that the words after the comma have leading spaces. How would I remove leading and trailing spaces in strings?

for reqSkill in string.gmatch(skillData[6], '([^,]+)') do

    print(reqSkill)

    -- table.insert(PreReqSkills, reqSkill)

    end

Is there a built in function for this?

1 answer

Log in to vote
1
Answered by 5 years ago

There's no built-in trim function, but there are multiple implementations on this page of the Lua-users Wiki.

At the bottom, a test shows that implementation number 5 seems to be favorable from a performance standpoint. (Not that this really matters in your case. It just gives us a reason to choose an implementation from the many on that page.)

function trim(s)
    return s:match'^%s*(.*%S)' or ''
end
Ad

Answer this question