Split Strings in Cocos2d-x Lua with Single or Multi-Character Delimiters In Game Development Implement a Cocos2d-x Lua string-split helper with string.find and string.sub that treats single or multi-character delimiters literally and returns an array. This helper splits a string using a literal delimiter and returns the pieces in an array: 123456789101112131415161718192021222324252627282930function string.split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if delimiter == '' then return false end local position = 1 local result = {} while true do local startPos, endPos = string.find( input, delimiter, position, true ) if not startPos then table.insert(result, string.sub(input, position)) break end table.insert(result, string.sub(input, position, startPos - 1)) position = endPos + 1 end return resultend Examples: 12345string.split("Hello,World", ",")-- {"Hello", "World"}string.split("Hello-+-World-+-Quick", "-+-")-- {"Hello", "World", "Quick"} The fourth argument to string.find is true, so the delimiter is treated as plain text rather than a Lua pattern. Donate WeChat Pay Alipay Post author: Nanfeng Post link: https://lengmo714.top/en/5b9b2fa4.html Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.