How to remove spaces from a string in Lua?

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

    It works, you just have to assign the actual result/return value. Use one of the following variations:

    str = str:gsub("%s+", "")
    str = string.gsub(str, "%s+", "")
    

    I use %s+ as there's no point in replacing an empty match (i.e. there's no space). This just doesn't make any sense, so I look for at least one space character (using the + quantifier).

提交回复
热议问题