Lua中过滤字符串中的不可见的控制字符
Lua中过滤字符串中的不可见的控制字符
在使用Lua处理一些字符串时,比如接收其他通信设备的一些数据的字符串中包含了一些特殊字符,这些字符是我们不需要的,需要把它过滤掉,防止数据异常。这个时候下面的这个接口就可以帮助你过滤这些不可见的控制字符,比如主要的是ASCII码0-32之间的字符
Code
--[[
@author:
@note: 保留n位小数
@param 需要过滤的字符串
@return 过滤后的字符串
@attention
--]]
function filter_spec_char(s)
local ss = {}
local k = 1
while true do
if k > #s then break end
local c = string.byte(s,k)
if not c then break end
if c<192 then
if (c>=33 and c<=126) then
table.insert(ss, string.char(c))
end
k = k + 1
end
end
return table.concat(ss)
end