如何从Lua中的字符串中删除空格? [英] How to remove spaces from a string in Lua?

查看:2583
本文介绍了如何从Lua中的字符串中删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Lua中的字符串中删除所有空格.这是我尝试过的:

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*", "")

这似乎不起作用.如何删除所有空格?

This does not seem to work. How can I remove all of the spaces?

推荐答案

它有效,您只需要分配实际结果/返回值即可.使用以下变体之一:

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+", "")

我使用%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).

这篇关于如何从Lua中的字符串中删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆