Search code examples
stringluasymbols

What is the "undetected" symbol in lua


a = string.sub("слово", 1,1) слово is a word in russian which needs 2 bytes for every letter and the output for print(a) is going to something like � depending on where you run it

if a =="?" then

if a =="�" then

if a =="" then if a ==nil then

doesn't work

I just want to know which symbol would work for the if statement


Solution

  • To select n-th symbol of UTF-8 string, use utf8.offset (Lua 5.3+ is required):

    local s = "слово"
    local n = 4
    local c = s:match(utf8.charpattern, utf8.offset(s,n))
    print(c)  -->  в
    if c == "в" then .... end