How to remove spaces from a string in Lua?

后端 未结 5 1180
臣服心动
臣服心动 2021-02-07 00:25

I want to remove all spaces from a string in Lua. This is what I have tried:

string.gsub(str, \"\", \"\")
string.gsub(str, \"% \", \"\")
string.gsub(str, \"%s*\"         


        
5条回答
  •  情歌与酒
    2021-02-07 00:57

    If anyone is looking to remove all spaces in a bunch of strings, and remove spaces in the middle of the string, this this works for me:

    function noSpace(str)
    
      local normalisedString = string.gsub(str, "%s+", "")
    
      return normalisedString
    
    end
    
    test = "te st"
    
    print(noSpace(test))
    

    Might be that there is an easier way though, I'm no expert!

提交回复
热议问题