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字符串分割函数[适配中文特殊符号混合]的更多相关文章

  1. hive函数 -- split 字符串分割函数

    hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...

  2. MSSQLSERVER数据库- 字符串分割函数返回类型表

    遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...

  3. 从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )

    首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...

  4. Split字符串分割函数

    非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...

  5. ASP.NET中常用的字符串分割函数

    asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...

  6. SQL点滴3—一个简单的字符串分割函数

    原文:SQL点滴3-一个简单的字符串分割函数 偶然在电脑里看到以前保存的这个函数,是将一个单独字符串切分成一组字符串,这里分隔符是英文逗号“,”  遇到其他情况只要稍加修改就好了 CREATE FUN ...

  7. Delphi 自带的字符串分割函数split

    下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...

  8. JavaScript中字符串分割函数split用法实例

    这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...

  9. SQL Server 自定义字符串分割函数

    一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数)   create function Func_StrArrayL ...

随机推荐

  1. SGU 107 987654321 problem【找规律】

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=107 题意: 平方后几位为987654321的n位数有多少个 分析: 虽然说是水题 ...

  2. sql查询报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题

    报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题 我的查询sql是 String sql2 =& ...

  3. 解释型语言Python

  4. POJ-3159_Candies

    Candies Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse ...

  5. oracle选择最有效率的表名顺序

    ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表 driving table)将被最先处理. 在FROM子句中包含多个表的情况下,你必须选择记录条 ...

  6. vue init定制团队模板之meta.js/meta.json写法入门

    在上一篇文章中,我们提到了meta.js,这次我们详细了解一下meta.js里面各个模块的写法. 对于 meta.js/metajson 文件, 目前主要字段如下: prompts<Object ...

  7. JVM问题诊断

    https://gitbook.cn/books/5d2d822f175a450263e945f9/index.html

  8. hdu 4419 Colourful Rectangle (离散化扫描线线段树)

    Problem - 4419 题意不难,红绿蓝三种颜色覆盖在平面上,不同颜色的区域相交会产生新的颜色,求每一种颜色的面积大小. 比较明显,这题要从矩形面积并的方向出发.如果做过矩形面积并的题,用线段树 ...

  9. 【CSS3】3D立方体动画

    关于CSS3的3D立方体动画 知识点: 1.每个元素有独立的坐标系 2.坐标系随当前元素的改变而发生改变 3.立方体由静态transform参数构成 4.通过给容器添加动画使立方体运动 效果图: &l ...

  10. gSOAP 使用

    1. wsdl2h 生成服务定义头文件 wsdl2h -o calc.h http://www.genivia.com/calc.wsdl 2. 从服务定义头文件calc.h生成客户端代码 soapc ...