给定一个带有一些空行的多行字符串,我如何遍历 Lua 中的行,包括空行?
local s = "foo\nbar\n\njim"
for line in magiclines(s) do
print( line=="" and "(blank)" or line)
end
--> foo
--> bar
--> (blank)
--> jim
此代码不包括空行:
for line in string.gmatch(s,'[^\r\n]+') do print(line) end
--> foo
--> bar
--> jim
此代码包含额外的虚假空行:
for line in string.gmatch(s,"[^\r\n]*") do
print( line=="" and "(blank)" or line)
end
--> foo
--> (blank)
--> bar
--> (blank)
--> (blank)
--> jim
--> (blank)