Chuyển tới nội dung chính

Tách chuỗi theo quy tắc phân cách (string.split)

Khai báo

parts = string.split(text, sep)

Tham số

  • text
    Chuỗi. Văn bản cần tách.
  • sep
    Chuỗi. Chuỗi phân cách.

Giá trị trả về

  • parts
    Bảng. Mảng các đoạn đã tách theo đúng thứ tự.

Mô tả

Tách một chuỗi theo quy tắc phân cách.
Từ khóa: tách chuỗi, tách văn bản.

Ví dụ

-- Ví dụ 1 (tách tài khoản/mật khẩu):
t = string.split('lfue6841214----123456', '----')
sys.alert('Account: '..t[1])
--
-- Ví dụ 2 (lấy văn bản giữa hai dấu #):
t = string.split('Hello, your verification code is #4937#, valid for 15 minutes. [ACME Corp]', '#')
sys.alert('Code: '..t[2])
--
-- Ví dụ 3 (trích xuất phần giữa phức tạp hơn):
t = string.split('Hello, your code is 4937, valid for 15 minutes. [ACME Corp]', 'code is ')
t = string.split(t[2], ', valid')
sys.alert('Code: '..t[1])

Lưu ý: Sử dụng hàm ngoài chương này: sys.alert

Ví dụ hàm bao 1

-- Trích xuất văn bản ở giữa (trả về nil nếu không tìm thấy)
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('Hello, your code is 4937, valid for 15 minutes. [ACME Corp]', 'is ', ', valid')
sys.alert('Code: '..r)
-- "4937"
--
r = str_middle('Hello, your verification code is #8346#, valid for 15 minutes. [ACME Corp]', '#')
sys.alert('Code: '..r)
-- "8346"

Lưu ý: Sử dụng hàm ngoài chương này: sys.alert

Ví dụ hàm bao 2

-- tên tệp
function str_strip_dirname(path)
local d = string.split(path, '/')
return d[#d]
end

-- tên thư mục
function str_strip_filename(path)
local d = string.split(path, '/')
d[#d] = nil
return table.concat(d, '/')
end

-- loại bỏ phần mở rộng của đoạn đường dẫn cuối cùng
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

-- lấy phần mở rộng của đoạn đường dẫn cuối cùng
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"

Lưu ý: Sử dụng các hàm ngoài chương này: sys.alert, table.concat

Ví dụ có liên quan (tách chuỗi UTF-8 gồm cả tiếng Trung và tiếng Anh thành từng ký tự, chỉ dành cho UTF-8)

-- Đây không phải ví dụ sử dụng 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"

Lưu ý: Sử dụng các hàm ngoài chương này: sys.alert, table.concat, utf8.char, utf8.codes

Ví dụ khác

Xem ở cuối chương này