lua的官方函数里无字符串分割,起初写了个简单的,随之发现如果是中文、字符串、特殊符号就会出现分割错误的情况,所以就有了这个zsplit.

  1. function zsplit(strn, chars)
  2. function stringPatch(str)
  3. --格式化输入包含特殊符号的分割字符
  4. local str_p = str: gsub("%)", "% %)")
  5. : gsub("%(", "%%(")
  6. : gsub("%[", "%%[")
  7. : gsub('%]', '%%]')
  8. : gsub('%:', '%%:')
  9. : gsub('%;', '%%;')
  10. : gsub('%+', '%%+')
  11. : gsub('%-', '%%-');
  12. return str_p;
  13. end
  14. function jbyteCount(jstr)
  15. local lenInByte = #jstr;
  16. local tbyteCount = {};
  17. local totallen = 0;
  18. for i = 1, lenInByte do
  19. --计算传入的字符串的每一个字符长度
  20. local curByte = string.byte(jstr, i);
  21. local byteCount = 0;--这里的初始长度设为0
  22. if curByte > 0 and curByte <= 127 then
  23. byteCount = 1
  24. elseif curByte >= 192 and curByte < 223 then
  25. byteCount = 2
  26. elseif curByte >= 224 and curByte < 239 then
  27. byteCount = 3
  28. elseif curByte >= 240 and curByte <= 247 then
  29. byteCount = 4
  30. end
  31. table.insert(tbyteCount,byteCount);
  32. totallen = totallen + byteCount;
  33. end
  34. -- print('totallen长度:',totallen);
  35. return totallen,tbyteCount;
  36. end
  37. --第二参数可省略 此时默认每个字符分割
  38. if not chars then
  39. chars = ''
  40. end
  41. --没有第一参数或为空值时报错
  42. if not strn then
  43. return "zsplit 错误: #1 nil 参数1为空值!";
  44. end
  45. local strSun = {};
  46. if chars == '' then
  47. --[[当默认每个字符分割时的补充方案.
  48. 因为遇到分割中文时,因为长度问题导致分割错误
  49. ]]
  50. local lenInByte = #strn;
  51. local width = 0
  52. local fuckn = 0
  53. for i = 1, lenInByte do
  54. --计算传入的字符串的每一个字符长度
  55. local curByte = string.byte(strn, i);
  56. local byteCount = 1;
  57. if curByte > 0 and curByte <= 127 then
  58. byteCount = 1
  59. elseif curByte >= 192 and curByte < 223 then
  60. byteCount = 2
  61. elseif curByte >= 224 and curByte < 239 then
  62. byteCount = 3
  63. elseif curByte >= 240 and curByte <= 247 then
  64. byteCount = 4
  65. end
  66. local char = string.sub(strn, i, i + byteCount - 1)
  67. fuckn = i + byteCount - 1;
  68. if (i~= fuckn or curByte < 127) then
  69. table.insert(strSun, char)
  70. end
  71. if (i == #strn) then
  72. return strSun
  73. end
  74. end
  75. else
  76. --endsign结束标志
  77. local endsign = 1;
  78. local ongsubs, gsubs = string.gsub(strn,stringPatch(chars), chars)
  79. print('\n替换结束:',ongsubs,
  80. '\n替换次数:',gsubs,
  81. '\n源字符串:',strn,
  82. '\n格式化匹配条件:',stringPatch(chars),
  83. '\n源匹配条件',chars)
  84. for i = 0,gsubs do
  85. local wi = string.find(ongsubs, stringPatch(chars));
  86. --print('匹配条件所在位置:',wi);
  87. if (wi == nil) then
  88. --当没有匹配到条件时 截取当前位置到最后一个位置
  89. wi = -1
  90. endsign = 0;
  91. end
  92. local acc = string.sub(ongsubs, 1, wi-endsign)
  93. table.insert(strSun,acc)-- (string.gsub(acc, stringPatch(chars), '')));
  94. ongsubs = string.sub(ongsubs, wi + jbyteCount(chars), -1);
  95. end
  96. end
  97. return strSun
  98. end

用法:

  1. zsplit(需要分割的字符串[string],分割条件[string])

让我们来测试一下:

  1. str = "中文:你好,哈嘻嘻哈。,英文:abcdefg,emoji表情
  2. lua字符串分割函数[适配中文特殊符号混合]的更多相关文章

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

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

      1. 从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )
      1. 首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...

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

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

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

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

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

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

    1. 随机推荐

        1. 创建ROS 工作空间时出现:程序“catkin_init_workspace”尚未安装,程序“catkin_make”尚未安装。
        1. 问题:创建ROS 工作空间时出现:程序“catkin_init_workspace”尚未安装,程序“catkin_make”尚未安装. 解决方法: source /opt/ros/kinetic/se ...

        1. @atcoder - AGC037F@ Counting of Subarrays
        1. 目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 L,连续至少 L 个相同的数 k 可以合并成 1 个 k+ ...

        1. Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(一)
        1. 一.Creating a trace or workload 注意点: In the Trace Properties dialog box, there is a checkbox option i ...

        1. @总结 - 10@ Miller-Rabin素性测试与Pollard-Rho因数分解
        1. 目录 @1 - 素性测试:Miller-Rabin算法@ @1.1 - 算法来源@ @1.2 - 算法描述@ @1.3 - 算法实现@ @2 - 因数分解:Pollard-Rho算法@ @2.0 - ...

        1. @hdu - 6598@ Harmonious Army
        1. 目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个士兵,每个士兵可以选择加入 A 组或 B 组. 有 m 个 ...

        1. 测试代码的执行时间魔法方法%time和%timeit
        1. 对于规模更大.运行时间更长的数据分析应用程序,你可能会希望测试一下各个部分或函数调用或语句的执行时间.你可能会希望了解某个复杂计算过程中到底是哪些函数占用的时间最多.幸运的是,在开发和测试代码的过程中 ...

        1. 洛谷 3177 [HAOI2015] 树上染色
        1. 题目描述 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后,你会获得黑点两两之 ...

        1. HTTP协议详解以及URL具体访问过程(转载)
        1. https://blog.csdn.net/f45056231p/article/details/82533490

        1. 基于jQuery+JSON的省市联动效果
        1. 省市区联动下拉效果在WEB应用中使用非常广泛,尤其在一些会员信息系统.电商网站最为常见,开发者一般使用AJAX实现无刷新下拉联动. 本文将讲述利用jQuery插件,通过读取JSON数据,实现无刷新动态 ...

        1. uva 624 CD (01背包)
        1.   CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...