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 字符串分割函数
hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...
- MSSQLSERVER数据库- 字符串分割函数返回类型表
遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...
- 从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )
首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...
- Split字符串分割函数
非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...
- ASP.NET中常用的字符串分割函数
asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...
- SQL点滴3—一个简单的字符串分割函数
原文:SQL点滴3-一个简单的字符串分割函数 偶然在电脑里看到以前保存的这个函数,是将一个单独字符串切分成一组字符串,这里分隔符是英文逗号“,” 遇到其他情况只要稍加修改就好了 CREATE FUN ...
- Delphi 自带的字符串分割函数split
下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...
- JavaScript中字符串分割函数split用法实例
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
- SQL Server 自定义字符串分割函数
一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数) create function Func_StrArrayL ...
随机推荐
- 关于IOS 微信浏览器 点击输入框自动放大问题
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0& ...
- Keycode对照表(键码对照表)
https://segmentfault.com/a/1190000005828048 字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S ...
- jq常用事件
https://www.cnblogs.com/sandraryan/ click(); 点击事件 dblclick(); 双击事件 $('.box').dblclick(function(){ al ...
- Python--day70--ORM多对多的三种方式
- 2019-9-2-win10-uwp-车表盘-径向规
title author date CreateTime categories win10 uwp 车表盘 径向规 lindexi 2019-09-02 12:57:38 +0800 2018-2-1 ...
- 学习java注意的地方
Java语言拼写上严格区分大小写: 一个Java源文件里可以定义多个Java类,但其中最多只能有一个类被定义成public类: 若源文件中包括了public类,源文件必须和该public类同名: 一个 ...
- 2019-9-2-给博客添加rss订阅
title author date CreateTime categories 给博客添加rss订阅 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17:23 ...
- H3C 单区域OSPF配置示例二
- Vue 项目构建完成 ----发布项目
发布项目 cmd 命令行 npm run build 执行打包文件 完成后就会有 3 个文件夹 分别是: 文件夹 :build config dist in ...
- PHP mysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理
相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析 PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理 介绍 mysqli是PHP程序与mysql数据库进行数据交互的桥梁, ...