Golang中删除多行字符串中的空行的惯用方法

huangapple go评论95阅读模式
英文:

Golang idiomatic way to remove a blank line from a multi-line string

问题

如果我有一个多行字符串,像这样:

这是一行

这是另一行

最好的方法是如何删除空行?我可以通过拆分、迭代和条件检查来实现,但是否有更好的方法?

英文:

If I have a multi line string like

this is a line

this is another line

what is the best way to remove the empty line? I could make it work by splitting, iterating, and doing a condition check, but is there a better way?

答案1

得分: 3

与ΔλЛ的答案类似,可以使用strings.Replace函数来实现:

// func Replace(s, old, new string, n int) string
// Replace函数返回将字符串s中的前n个非重叠的old实例替换为new后的副本。如果old为空,则它在字符串的开头和每个UTF-8序列之后匹配,从而为k个rune的字符串提供最多k+1个替换。如果n < 0,则替换的数量没有限制。

package main

import (
    "fmt"
    "strings"
)

func main() {

    var s = `line 1
line 2

line 3`

    s = strings.Replace(s, "\n\n", "\n", -1)

    fmt.Println(s)
}

你可以在这里查看代码示例:https://play.golang.org/p/lu5UI74SLo

英文:

Similar to ΔλЛ's answer it can be done with strings.Replace:

> func Replace(s, old, new string, n int) string
Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.

package main

import (
    &quot;fmt&quot;
    &quot;strings&quot;
)

func main() {

    var s = `line 1
line 2

line 3`

    s = strings.Replace(s, &quot;\n\n&quot;, &quot;\n&quot;, -1)

    fmt.Println(s)
}

https://play.golang.org/p/lu5UI74SLo

答案2

得分: 2

假设您希望输出与删除空行后的相同字符串,我会使用正则表达式:

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)
}

以下是翻译好的代码部分。

英文:

Assumming that you want to have the same string with empty lines removed as an output, I would use regular expressions:

import (
	&quot;fmt&quot;
	&quot;regexp&quot;
)

func main() {

	var s = `line 1
line 2

line 3`

	regex, err := regexp.Compile(&quot;\n\n&quot;)
	if err != nil {
		return
	}
	s = regex.ReplaceAllString(s, &quot;\n&quot;)

	fmt.Println(s)
}

答案3

得分: 1

以下是翻译好的内容:

更通用的方法可能是这样的。

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	s := `



	####

	####





	####

	####





	`

	fmt.Println(regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), "\n"))
}

https://play.golang.org/p/uWyHfUIDw-o

英文:

The more generic approach would be something like this maybe.

package main

import (
	&quot;fmt&quot;
	&quot;regexp&quot;
	&quot;strings&quot;
)

func main() {
	s := `
	
	
	#### 
	
	####
	
	
	
	
	####
	
	
	####
	
	
	
	
	`

	fmt.Println(regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), &quot;\n&quot;))
}

https://play.golang.org/p/uWyHfUIDw-o

答案4

得分: 0

为了去除所有空行,改进@syntagma的响应:

import (
    "fmt"
    "regexp"
)

func main() {

    var s = `line 1
line 2

line 3`

    regex, _ := regexp.Compile(`\n{2,}`)
    s = regex.ReplaceAllString(s, "\n")

    fmt.Println(s)
}
英文:

To improve @syntagma response in order to remove all empty lines:

import (
    &quot;fmt&quot;
    &quot;regexp&quot;
)

func main() {

    var s = `line 1
line 2

line 3`

    regex, _ := regexp.Compile(`\n{2,}`)
    s = regex.ReplaceAllString(s, &quot;\n&quot;)

    fmt.Println(s)
}

huangapple
  • 本文由 发表于 2016年2月12日 18:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/35360080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定