lua字符串分割函数[适配中文特殊符号混合]
lua的官方函数里无字符串分割,起初写了个简单的,随之发现如果是中文、字符串、特殊符号就会出现分割错误的情况,所以就有了这个zsplit.
function zsplit(strn, chars)
function stringPatch(str)
--格式化输入包含特殊符号的分割字符
local str_p = str: gsub("%)", "% %)")
: gsub("%(", "%%(")
: gsub("%[", "%%[")
: gsub('%]', '%%]')
: gsub('%:', '%%:')
: gsub('%;', '%%;')
: gsub('%+', '%%+')
: gsub('%-', '%%-');
return str_p;
end
function jbyteCount(jstr)
local lenInByte = #jstr;
local tbyteCount = {};
local totallen = 0;
for i = 1, lenInByte do
--计算传入的字符串的每一个字符长度
local curByte = string.byte(jstr, i);
local byteCount = 0;--这里的初始长度设为0
if curByte > 0 and curByte <= 127 then
byteCount = 1
elseif curByte >= 192 and curByte < 223 then
byteCount = 2
elseif curByte >= 224 and curByte < 239 then
byteCount = 3
elseif curByte >= 240 and curByte <= 247 then
byteCount = 4
end
table.insert(tbyteCount,byteCount);
totallen = totallen + byteCount;
end
-- print('totallen长度:',totallen);
return totallen,tbyteCount;
end
--第二参数可省略 此时默认每个字符分割
if not chars then
chars = ''
end
--没有第一参数或为空值时报错
if not strn then
return "zsplit 错误: #1 nil 参数1为空值!";
end
local strSun = {};
if chars == '' then
--[[当默认每个字符分割时的补充方案.
因为遇到分割中文时,因为长度问题导致分割错误
]]
local lenInByte = #strn;
local width = 0
local fuckn = 0
for i = 1, lenInByte do
--计算传入的字符串的每一个字符长度
local curByte = string.byte(strn, i);
local byteCount = 1;
if curByte > 0 and curByte <= 127 then
byteCount = 1
elseif curByte >= 192 and curByte < 223 then
byteCount = 2
elseif curByte >= 224 and curByte < 239 then
byteCount = 3
elseif curByte >= 240 and curByte <= 247 then
byteCount = 4
end
local char = string.sub(strn, i, i + byteCount - 1)
fuckn = i + byteCount - 1;
if (i~= fuckn or curByte < 127) then
table.insert(strSun, char)
end
if (i == #strn) then
return strSun
end
end
else
--endsign结束标志
local endsign = 1;
local ongsubs, gsubs = string.gsub(strn,stringPatch(chars), chars)
print('\n替换结束:',ongsubs,
'\n替换次数:',gsubs,
'\n源字符串:',strn,
'\n格式化匹配条件:',stringPatch(chars),
'\n源匹配条件',chars)
for i = 0,gsubs do
local wi = string.find(ongsubs, stringPatch(chars));
--print('匹配条件所在位置:',wi);
if (wi == nil) then
--当没有匹配到条件时 截取当前位置到最后一个位置
wi = -1
endsign = 0;
end
local acc = string.sub(ongsubs, 1, wi-endsign)
table.insert(strSun,acc)-- (string.gsub(acc, stringPatch(chars), '')));
ongsubs = string.sub(ongsubs, wi + jbyteCount(chars), -1);
end
end
return strSun
end
用法:
zsplit(需要分割的字符串[string],分割条件[string])
让我们来测试一下:
str = "中文:你好,哈嘻嘻哈。,英文:abcdefg,emoji表情
lua字符串分割函数[适配中文特殊符号混合]的更多相关文章
hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...
遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...
首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...
非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...
asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...
原文:SQL点滴3-一个简单的字符串分割函数 偶然在电脑里看到以前保存的这个函数,是将一个单独字符串切分成一组字符串,这里分隔符是英文逗号“,” 遇到其他情况只要稍加修改就好了 CREATE FUN ...
下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数) create function Func_StrArrayL ...
随机推荐
问题:创建ROS 工作空间时出现:程序“catkin_init_workspace”尚未安装,程序“catkin_make”尚未安装. 解决方法: source /opt/ros/kinetic/se ...
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 L,连续至少 L 个相同的数 k 可以合并成 1 个 k+ ...
一.Creating a trace or workload 注意点: In the Trace Properties dialog box, there is a checkbox option i ...
目录 @1 - 素性测试:Miller-Rabin算法@ @1.1 - 算法来源@ @1.2 - 算法描述@ @1.3 - 算法实现@ @2 - 因数分解:Pollard-Rho算法@ @2.0 - ...
目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个士兵,每个士兵可以选择加入 A 组或 B 组. 有 m 个 ...
对于规模更大.运行时间更长的数据分析应用程序,你可能会希望测试一下各个部分或函数调用或语句的执行时间.你可能会希望了解某个复杂计算过程中到底是哪些函数占用的时间最多.幸运的是,在开发和测试代码的过程中 ...
题目描述 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后,你会获得黑点两两之 ...
https://blog.csdn.net/f45056231p/article/details/82533490
省市区联动下拉效果在WEB应用中使用非常广泛,尤其在一些会员信息系统.电商网站最为常见,开发者一般使用AJAX实现无刷新下拉联动. 本文将讲述利用jQuery插件,通过读取JSON数据,实现无刷新动态 ...
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...