简体   繁体   English

有没有办法,将字符串中的空格更改为下划线?

[英]is there a way that works , to change spaces in a string to underscore?

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

i need to change space characters to underscore so i can find the file in termanal i have tried to use apis but it's not working for me due to my computer deletes it after install it. 我需要将空格字符更改为下划线,以便我可以在termanal中找到该文件我尝试使用api但由于我的计算机在安装后将其删除,因此它无效。 so i just need a function that can make spaces , underscore and ,use the termanal test command to find a file 所以我只需要一个可以创建空格,下划线的函数,并使用termanal test命令查找文件

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 _.

Example: 例:

print( ("Hello world"):gsub("%s+", "_") )
-- will print "Hello_world"

EDIT: Note that string.gsub() creates a new string instead of modifying the old one, which is why in my first example the reasignation str = str:gsub... was necessary. 编辑:请注意string.gsub()创建一个新的字符串而不是修改旧的字符串,这就是为什么在我的第一个例子中重新签名str = str:gsub...是必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文件名中用下划线更改空格 - Change spaces with underscore in filename 有没有办法在Swift中更改NSTextView容器的字符串值? - Is there a way to change the string value of an NSTextView container in Swift? 如何改变DVD播放器等空间? - how to change spaces like dvd player? Applescript改变osx桌面(空格)无法正常工作 - Applescript to change osx desktop (spaces) not working 用Bash方式删除大文件一列中的空格 - Bash way to remove spaces in one column of huge file 为什么不使用“ -a”参数在MacOS上进行展开就可以更改内部空间? - Why can unexpand on MacOS change internal spaces without the “-a” argument? 无需 killall Dock 即可在 Mac 上无缝更改所有桌面空间的壁纸 - Seamlessly change all desktop spaces' wallpaper on Mac without killall Dock 在字符串的两个空白字符(空格)之间剪切子字符串 - to cut a substring between two blank characters (spaces) of a string 字符串中导致致命错误的空格:不能超出endIndex - Spaces in string causing fatal error: cannot increment beyond endIndex OSX s换行符-为什么可以将空格转换为换行符,但不能将换行符转换为空格 - OSX sed newlines - why conversion of whitespace to newlines works, but newlines are not converted to spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM