最初の質問。文字列の最後の文字がマルチバイトでないかどうかを Lua で判断する最も簡単な方法は何ですか? または、文字列から最後の文字を削除する最も簡単な方法は何ですか?
有効な文字列の例と、関数の出力を希望するものを次に示します
hello there --- result should be: hello ther
anñ --- result should be: an
כראע --- result should be: כרא
ㅎㄹㅇㅇㅅ --- result should be: ㅎㄹㅇㅇ
次のようなものが必要です
function lastCharacter(string)
--- some code which will extract the last character only ---
return lastChar
end
またはそれがより簡単な場合
function deleteLastCharacter(string)
--- some code which will output the string minus the last character ---
return newString
end
これが私が進んできた道です
local function lastChar(string)
local stringLength = string.len(string)
local lastc = string.sub(string,stringLength,stringLength)
if lastc is a multibyte character then
local wordTable = {}
for word in string:gmatch("[\33-\127\192-\255]+[\128-\191]*") do
wordTable[#wordTable+1] = word
end
lastc = wordTable[#wordTable]
end
return lastc
end