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. 模板—FFT

    卷积:$C[i]=\sum \limits_{j=0}^{i}A[j]*B[i-j]$可以画图理解一下其实就是交叉相乘的和. 卷积可以看作两个多项式乘积的形式,只不过求出的结果的项数不同. FFT讲解 ...

  2. Mysql 数据库优化(一)

    一 避免网页访问错误 1  数据库连接timeout产生页面5xx错误 2 慢查询造成页面无法加载 3 阻塞造成数据无法提交 二 增加数据库的稳定性 三 优化用户体验 1 流畅的页面访问速度 2 良好 ...

  3. ural 1519 Formula 1(插头dp)

    1519. Formula 1 @ Timus Online Judge 干了一天啊!!!插头DP入门. 代码如下: #include <cstdio> #include <cstr ...

  4. POJ1664 放苹果

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...

  5. iptables 限制访问规则

    iptables -I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT把这条语句插在input链的最前面(第一条),对状态为ESTABLI ...

  6. 【codeforces 777E】Hanoi Factory

    [题目链接]:http://codeforces.com/problemset/problem/777/E [题意] 让你摆汉诺塔片; 要求在上面的片的外圈大于在下面的片的内圈,且小于下面的片的外圈; ...

  7. .net Framework 源代码 · ScrollViewer

    本文是分析 .net Framework 源代码的系列,主要告诉大家微软做 ScrollViewer 的思路,分析很简单. 看完本文,可以学会如何写一个 ScrollViewer ,如何定义一个 IS ...

  8. C++调用Lua编程环境搭建及测试代码示例

    C++调用Lua编程环境搭建及测试代码示例 摘要:测试环境是VS2005+LuaForWindows_v5.1.4-45.exe+WIN7 1.安装lua开发环境LuaForWindows_v5.1. ...

  9. springboot 项目打包可运行jar文件

    eclipse 运行run as  maven bulid  ,填入package ,运行打包 java -jar xxx.jar

  10. Python--day38---进程间通信--初识队列(multiprocess.Queue)之生产者,消费者模型

    1,生产者消费者模型.py import random import time from multiprocessing import Queue, Process def producer(name ...