Skip to content

Instantly share code, notes, and snippets.

@tekkub
Created October 28, 2009 23:51
Show Gist options
  • Select an option

  • Save tekkub/220990 to your computer and use it in GitHub Desktop.

Select an option

Save tekkub/220990 to your computer and use it in GitHub Desktop.
local function utf8sub(str, start, numChars)
local currentIndex = start
while numChars > 0 and currentIndex <= #str do
local char = string.byte(str, currentIndex)
if char >= 240 then currentIndex = currentIndex + 4
elseif char >= 225 then currentIndex = currentIndex + 3
elseif char >= 192 then currentIndex = currentIndex + 2
else currentIndex = currentIndex + 1 end
numChars = numChars - 1
end
return str:sub(start, currentIndex - 1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment