Il existe une erreur de syntaxe sur votre extrait de code qui provoque l'erreur que vous avez mentionnée. La fonction gsub fonctionne vraiment bien.
text = "ib c e d f"
text = string.lower(text)
b = text:gsub("%s+", "_") --> this gsub is working just fine
for k=1, #b do
if not string.sub(b, k,k) or string.sub(b, k,k) ~= " " then
if a[i][2] == string.sub(b, k,k) then --> you didn't assign any table called "a" before --> attempting to index a nil value
print(yes)
end
--> haven't close the "if not string.sub" function with an end
end --> this is the end for the loop "for k"
Je suis juste deviner que d'une manière extravagante vous voulez comparer la chaîne de résultat avec la chaîne d'origine. Étant donné que votre question est si énigmatique, je ne pouvais vous offrir ce code ci-dessous pour votre référence:
text = "ab c d e f "
text = string.lower(text)
output = text:gsub("%s", "_")
for k = 1, #output do
local char = string.sub(output, k, k)
local originalChar = string.sub(text, k, k)
if (originalChar ~= " ") and (char == originalChar) then
print(char .. " --> OK")
end
end
Le motif gsub utilise %s
au lieu de %s+
de sorte que chaque espace est converti en un trait de soulignement pour permettre simple test de l'unité (char par comparaison de chars). Extrait de code disponible here.
qu'est-ce que 'a [i] [2]'? – hjpotter92