2017-02-11 3 views
-2

を働いていないために、私はホワイトスペースを変更するにはGSUBを使用していても、私はいただきました!problem.iのカント不思議それは私の先生が教えた指示なので、ペアを使用してください。使用string.gsubは_

これは初心者の方にはごめんなさい。

text = "ib c e d f" 
text = string.lower(text) 
b = text:gsub("%s+", "_") 
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 
     print(yes) 
     end 
    end 
+1

「a [i] [2]」とは何ですか? – hjpotter92

答えて

0

あなたが言及したエラーの原因となるコードスニペットに構文エラーがあります。 gsub関数は実際にうまく動作しています。

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" 

私は乱暴にあなたが元の文字列と結果の文字列を比較したいことを推測しています。あなたの質問はとても謎めいている、私はあなたの参照のために以下のコードを唯一提供できるので:

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 

各スペースは、簡単なユニットテスト(文字を許可するようにアンダースコアに変換されるように、GSUBパターンではなく%s+%sを使用していますchar比較による)。利用可能なコードスニペットhere