@renato.bugge
It works but does not return the right value, I discovered this while working with textfields. I found a utf8 library that takes care of string.len() but not string.upper/lower().
If you would create text edit app and would want to know the character count len() would not return the correct count, however bytewise it is correct.
Run this.
local txt1 = "åäöÅÄÖ" -- len return 12, string.upper/lower() does not work.
local txt2 = "abcABC" -- len return 6, string.upper/lower() does work.
local lower = display.newText("txt1 (åäöÅÄÖ) to Lower: "..string.lower(txt1), 30, 150, 260, 0, "Helvetica", 16)
local upper = display.newText("txt2 (abcABC) to Upper "..string.upper(txt2), 30, 190, 260, 0, "Helvetica", 16)
local len = display.newText("txt1 Len: "..string.len(txt1).."\ntxt2 Len: "..string.len(txt2), 30, 230, 260, 0, "Helvetica", 16)
Utf8 functions from the lib that fix the len() issue.
[code]
function utf8charbytes (s, i)
– argument defaults
i = i or 1
local c = string.byte(s, i)
– determine bytes needed for character, based on RFC 3629
if c > 0 and c <= 127 then
– UTF8-1
return 1
elseif c >= 194 and c <= 223 then
– UTF8-2
local c2 = string.byte(s, i + 1)
return 2
elseif c >= 224 and c <= 239 then
– UTF8-3
local c2 = s:byte(i + 1)
local c3 = s:byte(i + 2)
return 3
elseif c >= 240 and c <= 244 then
– UTF8-4
local c2 = s:byte(i + 1)
local c3 = s:byte(i + 2)
local c4 = s:byte(i + 3)
return 4
end
end
– returns the number of characters in a UTF-8 string
function utf8len (s)
local pos = 1
local bytes = string.len(s)
local len = 0
while pos <= bytes and len ~= chars do
local c = string.byte(s,pos)
len = len + 1
pos = pos + utf8charbytes(s, pos)
end
if chars ~= nil then
return pos - 1
end
return len
end
[/code] [import]uid: 13560 topic_id: 24285 reply_id: 141530[/import]