Last active
May 24, 2024 00:36
-
-
Save akornatskyy/63100a3e6a971fd13456b6db104fb65b to your computer and use it in GitHub Desktop.
Lua: How to separate string with comma
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function split_with_comma(str) | |
| local fields = {} | |
| for field in str:gmatch('([^,]+)') do | |
| fields[#fields+1] = field | |
| end | |
| return fields | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local function split_with_comma(str) | |
| local r = {} | |
| local i = 1 | |
| local p = 1 | |
| while true do | |
| local s = str:find(',', p, true) | |
| if not s then | |
| r[i] = str:sub(p) | |
| break | |
| end | |
| r[i] = str:sub(p, s - 1) | |
| p = s + 1 | |
| i = i + 1 | |
| end | |
| return r | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment