Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Lua中删除字符串中的空格?_Lua_Replace - Fatal编程技术网

如何在Lua中删除字符串中的空格?

如何在Lua中删除字符串中的空格?,lua,replace,Lua,Replace,我想删除Lua中字符串中的所有空格。这就是我尝试过的: string.gsub(str, "", "") string.gsub(str, "% ", "") string.gsub(str, "%s*", "") 这似乎不起作用。如何删除所有空格?它可以工作,您只需指定实际结果/返回值。使用以下变体之一: str = str:gsub("%s+", "") str = string.gsub(str, "%s+", "") 我使用%s+,因为没有必要替换空匹配项(即没有空格)。这根本没有任

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

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

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

它可以工作,您只需指定实际结果/返回值。使用以下变体之一:

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

我使用
%s+
,因为没有必要替换空匹配项(即没有空格)。这根本没有任何意义,所以我至少查找一个空格字符(使用
+
量词)。

最快的方法是使用trim.so从trim.c编译而来:

/* trim.c - based on http://lua-users.org/lists/lua-l/2009-12/msg00951.html
            from Sean Conner */
#include <stddef.h>
#include <ctype.h>
#include <lua.h>
#include <lauxlib.h>

int trim(lua_State *L)
{
 const char *front;
 const char *end;
 size_t      size;

 front = luaL_checklstring(L,1,&size);
 end   = &front[size - 1];

 for ( ; size && isspace(*front) ; size-- , front++)
   ;
 for ( ; size && isspace(*end) ; size-- , end--)
   ;

 lua_pushlstring(L,front,(size_t)(end - front) + 1);
 return 1;
}

int luaopen_trim(lua_State *L)
{
 lua_register(L,"trim",trim);
 return 0;
}
更详细(与其他方法相比):

用法:

local trim15 = require("trim")--at begin of the file
local tr = trim("   a z z z z z    ")--anywhere at code

您可以使用以下功能:

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

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

str=" aa " 
print(all_trim(str) .. "e")
输出为:

aae

对于LuaJIT,来自LuaWiki的所有方法(可能除了原生C/C++)在我的测试中都非常慢。此实现显示了最佳性能:

function trim (str)
  if str == '' then
    return str
  else  
    local startPos = 1
    local endPos   = #str

    while (startPos < endPos and str:byte(startPos) <= 32) do
      startPos = startPos + 1
    end

    if startPos >= endPos then
      return ''
    else
      while (endPos > 0 and str:byte(endPos) <= 32) do
        endPos = endPos - 1
      end

      return str:sub(startPos, endPos)
    end
  end
end -- .function trim
功能微调(str)
如果str=='',那么
返回str
其他的
本地startPos=1
本地endPos=#str
而(startPos(Endos>0和STR:Byter(EndoPs)

如果有人想删除一串字符串中的所有空格,并在字符串中间删除空格,这对我来说是有效的:

function noSpace(str)

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

  return normalisedString

end

test = "te st"

print(noSpace(test))

可能有一种更简单的方法,我不是专家!

你真的不需要使用+,如果你只使用%s,它将不匹配非空格。使用%s似乎更常见-虽然我猜最终结果是一样的。最终结果将是一样的,但是使用
+
你可以一次替换后面的空格,这可能会更好RMAT(不确定它在Lua中是否真的很重要)。我也不知道。只是值得一提。或者更简短一次:
s:match(“^%s*(.-)%s*$”)
。我使用了你的建议,但我得到了
标准:1:尝试连接一个nil值
我给出的代码对任何输入字符串都有效,并且不会产生
nil
结果。你返回结果了吗?或者你忘记了
“e”
周围的引号了吗?好的,这里是复制/粘贴的完整代码:
函数all\u trim(s)返回s:match(“^%s*(.-)%s*$”)end;str=“aa”print(all_trim(str)…“e”)
。这在这里可以正常工作。您忘记了返回。
function noSpace(str)

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

  return normalisedString

end

test = "te st"

print(noSpace(test))