如果我有一个多行字符串
this is a line
this is another line
Run Code Online (Sandbox Code Playgroud)
删除空行的最佳方法是什么?我可以通过拆分、迭代和进行条件检查来使其工作,但是有更好的方法吗?
假设您想要删除空行的相同字符串作为输出,我将使用正则表达式:
import (
"fmt"
"regexp"
)
func main() {
var s = `line 1
line 2
line 3`
regex, err := regexp.Compile("\n\n")
if err != nil {
return
}
s = regex.ReplaceAllString(s, "\n")
fmt.Println(s)
}
Run Code Online (Sandbox Code Playgroud)