我很惊讶我在StackOverflow或Lua.org网站上没有找到这个明确的答案
如果
然后,我如何操纵该变量从"J"变为"K"?
我查看了Lua.Org网站上的字符串库,并没有在那里的任何页面上看到"字母表"这个词
例如
--------------------------------------------------
-- --
-- Func Name: Alphabetic_Advance_By_1 --
-- --
-- On Entry: The_Letter contains one --
-- character. It must be a --
-- letter in the alphabet. --
-- --
-- On Exit: The caller receives the --
-- next letter --
-- --
-- e.g., --
-- --
-- A will return B --
-- B will return C --
-- C will return D --
-- --
-- X will return Y --
-- Y will return Z --
-- Z will return A --
-- --
-- --
-- --
--------------------------------------------------
function Alphabetic_Advance_By_1(The_Letter)
local Temp_Letter
Temp_Letter = string.upper(The_Letter)
-- okay, what goes here ???
return(The_Answer)
end
Run Code Online (Sandbox Code Playgroud)
我很惊讶我在StackOverflow或Lua.org网站上没有找到这个明确的答案
它与您在特定构建的Lua中使用的字符编码有关,因此它超出了Lua语言的范围.
您可以使用string.byte获取字符串的字节.您可以使用string.char将字节转换为字符串.
Lua不保证'A'到'Z'的字符代码是连续的,因为C没有.您甚至不能确定每个都是用单个字节编码的.如果您的实现使用ASCII,则每个字符由单个字节值表示,您可以添加1获取下一个字母,但您不应该依赖于此.例如,如果Temp_Letter <'Z':
The_Answer = string.char(Temp_Letter:byte() + 1)
Run Code Online (Sandbox Code Playgroud)
这是一种不依赖字符编码的方法:
local alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
local index = alphabet:find(Temp_Letter)
if index then
index = (index % #alphabet) + 1 -- move to next letter, wrapping at end
TheAnswer = alphabet:sub(index, index)
end
Run Code Online (Sandbox Code Playgroud)