Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would you remove trailing and leading whitespace?

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.