Why use string.sub()
to cut Chinese characters and appear garbled?
Is this a bug?
My English is not good so I use Google translate
Why use string.sub()
to cut Chinese characters and appear garbled?
My English is not good so I use Google translate
I think it’s a bug with the font because whenever that appears it means the character is unsupported by the font.
So how should I solve it? Is there any other code for roblox to cut strings?
Not really a bug, just has to do with how some UTF-8 characters are encoded in the structure.
A character in a string can usually be represented as 1 byte (8 bits/1 character), but a character like this (的), takes up three bytes (3 characters). String.sub(m, n)
returns a substring from the m-th character to the nth character, so you’re trying to get only the first two characters as opposed to the three that make up the character. So, in theory string.sub("的", 1, 3))
works and it does actually work because it returns that character. If you did string.sub("的", 1, 1))
, you get an invalid utf code so it returns this �
(U+FFFD]: commonly used as a replacement character for a character that could not be recognised or is invalid)
The utf8 library can help. For example: If it’s that you want to get the first, whole character then using the utf8.graphemes function helps:
local str = "丁"
str ..= "专"
str ..= "的"
function GetChar(str, index)
local iter = utf8.graphemes(str)
for i = 1, index - 1 do
iter()
end
local i, j = iter()
return str:sub(i, j)
end
print( GetChar(1) ) -- "丁"
Not sure what you’re trying to do here but that’s just something to know