Macos 有没有办法把字符串中的空格改成下划线?
我需要将空格字符改为下划线 所以我可以在termanal中找到该文件 我曾尝试使用API,但它不适合我,因为我的计算机在安装后会删除它。所以我只需要一个函数,它可以生成空格、下划线和,使用termanal test命令来查找文件Macos 有没有办法把字符串中的空格改成下划线?,macos,lua,terminal,Macos,Lua,Terminal,我需要将空格字符改为下划线 所以我可以在termanal中找到该文件 我曾尝试使用API,但它不适合我,因为我的计算机在安装后会删除它。所以我只需要一个函数,它可以生成空格、下划线和,使用termanal test命令来查找文件 function exists(f) filetry="" local fileBuffer={} for w in x:gmatch("%S+") do table.insert(fileBuffer,w) end
function exists(f)
filetry=""
local fileBuffer={}
for w in x:gmatch("%S+") do
table.insert(fileBuffer,w)
end
for i, v in ipairs(fileBuffer) do
filetry=filetry.."_"..v
end
f=filetry
if os.execute("test -e "..f) == true then
return true
else
return false
end
end
例如:
str = str:gsub("%s+", "_")
-- where `str` is the string you want to remove the spaces from.
-- Replaces multiple consecutive space characters with single _.
-- Remove the `+` to make it replace each space character with its own _.
编辑:请注意,string.gsub()创建一个新字符串而不是修改旧字符串,这就是为什么在我的第一个示例中需要重新指定
str=str:gsub…
。我建议将“test-e”。.f
替换为(“test-e%s”):格式(f)
当字符串最终变长并在一行代码中包含8个串联时,更易于阅读。如果我没有弄错的话,它也应该快一点。我还建议更改如果variable==true,则返回true,否则返回false end
构造一个简单的返回变量
,或者如果它是一个返回多个参数的函数,则首先将其保存在变量中,然后执行local result=os.execute(…);返回结果代码>。这使得代码不那么混乱,更易于阅读。
print( ("Hello world"):gsub("%s+", "_") )
-- will print "Hello_world"