DrA*_*rAl 28
自8.0.1630 vim以来trim()
.
对于旧版本:假设您尝试在vimscript中的变量上执行此操作,您可以执行以下操作:
let new_var = substitute(var, '^\s*\(.\{-}\)\s*$', '\1', '')
Run Code Online (Sandbox Code Playgroud)
如果你愿意,你可以随时让自己的功能:
function! Strip(input_string)
return substitute(a:input_string, '^\s*\(.\{-}\)\s*$', '\1', '')
endfunction
let new_var = Strip(var)
Run Code Online (Sandbox Code Playgroud)
小智 7
从8.0.1630开始, vim具有内置trim()
功能来执行此操作。从文档:
trim({text}[, {mask}])
Run Code Online (Sandbox Code Playgroud)Return {text} as a String where any character in {mask} is removed from the beginning and end of {text}. If {mask} is not given, {mask} is all characters up to 0x20, which includes Tab, space, NL and CR, plus the non-breaking space character 0xa0. This code deals with multibyte characters properly.
因此,调用trim(var)
会从中删除开头和结尾的空格var
。