Skip to content

Commit 62d516d

Browse files
committed
LUA按照指定字符分割字符串
1 parent 020c732 commit 62d516d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lua_string_to_table.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--在LUA中,没有类似python的str.split()函数,如果要将一个字符串按照指定的符号分割,并存到table中。
2+
--如将a="1,2,3"转换为{1,2,3}
3+
4+
a="1,2,3,4," --或者 a="1,2,3,4" 不论最后一个是否有","
5+
t={}
6+
7+
for w in string.gmatch(a,"([^',']+)") do --按照“,”分割字符串
8+
table.insert(t,w)
9+
end
10+
11+
for k,v in ipairs(t) do
12+
print(k..":"..v)
13+
end

0 commit comments

Comments
 (0)