Deliminate


Method 1

This splits a given string into a table using a specified single character delimiter.

Example usage: splitTable=SplitString('First part|Second part', '|')

function SplitString(inString, inDelimiter)
assert(inDelimiter:len() == 1, 'SplitString: Delimiter may only be a single character')
local outTable = {}
for matchedString in inString:gmatch('[^%' .. inDelimiter .. ']+') do
table.insert(outTable, matchedString)
end
return outTable
end

Method 2

This splits a given string into a table using a specified single or multiple character delimiter.

Example usage: splitTable=SplitString('First part||Second part', '||')

function SplitString(inString, inPattern)
local outTable = {}
local findPattern = '(.-)' .. inPattern
local lastEnd = 1
local currentStart, currentEnd, foundString = inString:find(findPattern, 1)
while currentStart do
if currentStart ~= 1 or foundString ~= '' then
table.insert(outTable, foundString)
end
lastEnd = currentEnd + 1
currentStart, currentEnd, foundString = inString:find(findPattern, lastEnd)
end
if lastEnd <= #inString then
foundString = inString:sub(lastEnd)
table.insert(outTable, foundString)
end
return outTable
end