Split a string by separator rule (string.split)
Declaration
parts = string.split(text, sep)
Parameters
- text
string. Text to split. - sep
string. Separator string.
Returns
- parts
table. Array of split segments in order.
Description
Split a string by a separator rule.
Keywords: split string, split text.
Examples
-- Example 1 (split account/password):
t = string.split('lfue6841214----123456', '----')
sys.alert('Account: '..t[1])
--
-- Example 2 (get text between two # marks):
t = string.split('您好,验证码是#4937#,15分钟内有效。【爆炸科技】', '#')
sys.alert('Code: '..t[2])
--
-- Example 3 (a more complex middle extraction):
t = string.split('您好,验证码是4937,15分钟内有效。【爆炸科技】', '验证码是')
t = string.split(t[2], ',15分钟')
sys.alert('Code: '..t[1])
Note: Uses function outside of this chapter: sys.alert
Wrapper example 1
-- Extract middle text (return nil if not found)
function str_middle(str, sep1, sep2)
assert(type(str) == 'string', '`str_middle` #1 must be string')
assert(type(sep1) == 'string', '`str_middle` #2 must be string')
assert(type(sep2) == 'nil' or type(sep2) == 'string', '`str_middle` #3 optional but must be string')
local t = string.split(str, sep1)
if not sep2 or sep1==sep2 then
return t[2]
else
if t[2] == nil then
return nil
else
t = string.split(t[2], sep2)
if t[2] == nil then
return nil
else
return t[1]
end
end
end
end
--
r = str_middle('您好,验证码是4937,15分钟内有效。【爆炸科技】', '码是', ',15分')
sys.alert('Code: '..r)
-- "4937"
--
r = str_middle('您好,验证码是#8346#,15分钟内有效。【爆炸科技】', '#')
sys.alert('Code: '..r)
-- "8346"
Note: Uses function outside of this chapter: sys.alert
Wrapper example 2
-- basename
function str_strip_dirname(path)
local d = string.split(path, '/')
return d[#d]
end
-- dirname
function str_strip_filename(path)
local d = string.split(path, '/')
d[#d] = nil
return table.concat(d, '/')
end
-- strip extension of last path segment
function str_strip_extension(path)
local d = string.split(path, '/')
local fnt = string.split(d[#d], '.')
d[#d] = fnt[1]
return table.concat(d, '/')
end
-- get extension(s) of last path segment
function str_get_extension(path)
local d = string.split(path, '/')
local fnt = string.split(d[#d], '.')
table.remove(fnt, 1)
return table.concat(fnt, '.')
end
sys.alert(str_strip_dirname("/private/var/mobile/Media/1ferver/lua/scripts/1.lua.xxt"))
-- "1.lua.xxt"
sys.alert(str_strip_filename("/private/var/mobile/Media/1ferver/lua/scripts/1.lua.xxt"))
-- "/private/var/mobile/Media/1ferver/lua/scripts"
sys.alert(str_strip_extension("/private/var/mobile/Media/1ferver/lua/scripts/1.lua.xxt"))
-- "/private/var/mobile/Media/1ferver/lua/scripts/1"
sys.alert(str_get_extension("/private/var/mobile/Media/1ferver/lua/scripts/1.lua.xxt"))
-- "lua.xxt"
sys.alert(str_strip_extension(str_strip_dirname("/private/var/mobile/Media/1ferver/lua/scripts/1.lua.xxt")))
-- "1"
Note: Uses functions outside of this chapter: sys.alert
, table.concat
Possibly related example (explode mixed Chinese/English UTF-8 string into characters)
-- Not an example using string.split
function text_explode(text)
local ret = {}
for p, c in utf8.codes(text) do
ret[#ret + 1] = utf8.char(c)
end
return ret
end
--
local t = text_explode('你好,XXTouch')
sys.alert(table.concat(t, '/')) -- "你/好/,/X/X/T/o/u/c/h"
Note: Uses functions outside of this chapter: sys.alert
, table.concat, utf8.char, utf8.codes