Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组
2016-09-01 20:21
549 查看
// 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 // @function [parent=#string] split // @param string input 输入字符串 // @param string delimiter 分割标记字符或字符串 // @return array#array 包含分割结果的数组 /* 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 local input = "Hello,World" local res = string.split(input, ",") -- res = {"Hello", "World"} local input = "Hello-+-World-+-Quick" local res = string.split(input, "-+-") -- res = {"Hello", "World", "Quick"} */ function string.split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter=='') then return false end local pos,arr = 0, {} -- for each divider found for st,sp in function() return string.find(input, delimiter, pos, true) end do table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end
相关文章推荐
- 编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格,和其他字符的个数,在主函数中输入字符串以及输出上述统计的结果。再考虑将算的的结果放在一个数组中
- php把一个字符串分割成字符数组(可以用参数指定数组每个元素字符的长度)
- sql按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便
- 字符串分割为字符串数组 去除前后指定字符
- 练习 2-5 编写函数 any(s1, s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。
- php把字符串指定字符分割成数组的方法
- 按照指定的字符串分割字符串形成字符数组
- C 语言 实现 字符串 分割 函数(返回"二维字符数组",及分割后的字符数组的长度)
- 写一个函数,用来把一个字符串用指定的字符作为分隔符分割成若干个子串输出 | 写一个函数,用来返回一个字符串中重复出现的最长字串的长度及其开始地址
- 练习 2-5 编写函数 any(s1, s2),将字符串 s2 中的任一字符在字符串 s1 中第一次 出现的位置作为结果返回。如果 s1 中不包含 s2 中的字符,则返回-1。
- 输入字符串,分割字符,分别显示结果
- oracle怎么切割一个指定的字符串按指定的标志符分割成字符数组
- ORACLE(PL/SQL) 根据字符分割(SPLIT)字符串返回数组
- 按指定的字符把字符串分割为字符串数组
- php把字符串指定字符分割成数组
- 练习 2-5 编写函数 any(s1, s2),将字符串 s2 中的任一字符在字符串 s1 中第一次 出现的位置作为结果返回。如果 s1 中不包含 s2 中的字符,则返回-1
- 练习2-5 编写函数any(s1,s2),将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2的字符,则返回-1。
- php把字符串指定字符分割成数组
- javascript将字符串按指定的字符分割成数组的函数split
- Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)