1

I am using Lua making an app about phonetic alphabet learning game, I have a question that I need to check the length of the phonetic transcription and need to check is it match what I typed, but I found all phonetic alphabet's length are 2 and, and they cannot match what I typed, the example is as below:

print(string.len("ð")       -- outcome is: 2
print(string.len("pɛt"))    -- outcome is: 4
print(string.sub("pɛt",3))  -- outcome is:›t

what should I do?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Noel Chung
  • 75
  • 1
  • 8

3 Answers3

2

The string library does not support UTF-8 strings. Try the utf8 library.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • people said UTF8 and UTF-8 have no difference, thanks anyway @.@ I tried to save the Lua file by other encodings e.g. UTF-16 in TextWrangler, when I run the apps at simulator, it show errors. – Noel Chung Apr 05 '15 at 12:17
2

Lua 5.2 has no native support for non-ASCII strings. Lua 5.3 has basic UTF-8 support, so maybe you should try and use Lua 5.3.

CinchBlue
  • 6,046
  • 1
  • 27
  • 58
  • You cannot change the version of Lua that Corona SDK uses. – Rob Miracle Feb 10 '14 at 2:17 <= someone said this at http://stackoverflow.com/questions/21632256/how-to-find-which-version-of-lua-do-i-use , as I am using Corona SDK simulator to see the output of my apps, are there any other methods to update its lua version or any other simulator could introduce to me? – Noel Chung Apr 05 '15 at 12:12
1

Thanks lhf and Cinch, by the hints of you, I finally figured out how to do, and the code is as below, please let me know if there are other smarter methods.

a. by calculating the length of the phonetic transcription, I just the original length minus the the non-single byte alphabet:

string.len("pɛt")-string.len("pɛt", "[^\128-\193]", ""))

b. by checking is it same as what I entered, I used string.gfind:

for tempLetter in string.gfind("pɛt", "([%z\1-\127\194-\244][\128-\191]*)") do
        if theLetterITyped == tempLetter then
              --content
        else
              --content
        end
    end
Noel Chung
  • 75
  • 1
  • 8