lua에서 여러 줄 문자열을 단일 줄 문자열로 어떻게 만들 수 있습니까?
나는 이것을 시도했다 :
function removeMultilines(str)
return str:gsub("\n")
end
하지만 작동하지 않습니다.
그러니까 기본적으로, 나는 회전 할 것 str1
으로str2
local str1 = [[Hey,
how are you today?
Whats your name?]]
local str2 = "Hey, how are you today? Whats your name?"
내가 어떻게 접근 할 수 있는지 아는 사람 있나요? 어떤 도움이라도 환영합니다.
Lua 5.3 참조 설명서 : string.gsub (s, pattern, repl [, n])
패턴 (§6.4.1 참조)의 모든 (또는 제공된 경우 처음 n) 발생이 repl에 의해 지정된 대체 문자열 (문자열, 테이블 또는 a)로 대체 된 s의 복사본을 반환합니다. 함수. gsub는 또한 발생한 총 일치 수를 두 번째 값으로 반환합니다. gsub라는 이름은 Global SUBstitution에서 유래되었습니다.
루아 프로그래밍 의 맨 아래 에서 다룹니다 : 20.1 – 패턴 일치 함수 는 다음 예제 중 하나입니다.
s = string.gsub("Lua is cute", "cute", "great")
print(s) --> Lua is great
를 올바르게 사용 string.gsub
하려면 문자열, 패턴 "\n"
및 값이 일치 항목을 " "
.
local str2 = str1:gsub("\r?\n", " ") --`\n` signifies the newline character
--`\r?` add some handling for different line endings
또는 string.gmatch 를 사용할 수 있지만 string.gsub
대체에 더 적합합니다.
귀하의 경우에 대한 적절한 사용은 다음과 같이 보일 수 있습니다.
function removeMultilines(str)
local lines = str:gmatch("([^\r\n]+)\r?\n?")
local output = lines()
for line in lines do
output = output .. " " .. line
end
print(output)
return output
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다