Created
February 9, 2015 09:07
-
-
Save leonlee/d5bfa26e36b62a4ae2ba to your computer and use it in GitHub Desktop.
tome4 chn 英文空格保留
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 删除字符串中的空格和制表符 | |
function delSpaceTxt(txt) | |
local chr = txt | |
local chr_new = "" | |
local len_chr = chr:len() | |
local cur_char | |
for i = 1 , len_chr do | |
cur_char = chr:sub(i,i) | |
if cur_char ~= " " and cur_char ~= "\t" then | |
chr_new = chr_new..cur_char | |
elseif i~=1 and chr:sub(i-1,i-1) == '%w' then | |
chr_new = chr_new..cur_char | |
-- 前置非中文,保留空格 | |
elseif i > 1 and string.byte(chr, i-1) < 128 then | |
chr_new = chr_new..cur_char | |
-- 后置非中文,保留空格 | |
elseif i < len_chr and string.byte(chr, i+1) < 128 then | |
chr_new = chr_new..cur_char | |
end | |
end | |
return chr_new | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment