문제

I have a word and I want to check if it's equal to another word. If it's so then all is right, but it can be right also if there's one (and only one) wrong character.

local word = "table"
local word2 = "toble"
if word == word2 then
  print("Ok")
end

How can I split word2?

도움이 되었습니까?

해결책

You can compare the string's length first, if they are equal then compare from the first character, if there's one character that is different, then the rest must be the same for your condition to be true:

function my_compare(w1, w2)
    if w1:len() ~= w2:len() then
        return false
    end
    for i = 1, w1:len() do
        if w1:sub(i, i) ~= w2:sub(i, i) then
            return w1:sub(i + 1) == w2:sub(i + 1)
        end
    end
    return true
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top