How to remove spaces from a string in Lua?

后端 未结 5 1179
臣服心动
臣服心动 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:46

    You use the following function :

    function all_trim(s)
      return s:match"^%s*(.*)":match"(.-)%s*$"
    end
    

    Or shorter :

    function all_trim(s)
       return s:match( "^%s*(.-)%s*$" )
    end
    

    usage:

    str=" aa " 
    print(all_trim(str) .. "e")
    

    Output is:

    aae
    

提交回复
热议问题