2017-02-11 5 views
-2

작동하지 _에 공백을 변경 뭐죠 문제 야의 캔트 나의 교사가 준 지시이기 때문에 쌍을 사용하십시오.사용 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

무엇이'[[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입니다.