英文:
How to strings.Split on newline?
问题
我正在尝试执行一个相当简单的任务,即按换行符拆分字符串。
这种方法不起作用:
temp := strings.Split(result,`\n`)
我还尝试了使用'而不是`,但没有成功。
有什么想法吗?
英文:
I'm trying to do the rather simple task of splitting a string by newlines.
This does not work:
temp := strings.Split(result,`\n`)
I also tried ' instead of ` but no luck.
Any ideas?
答案1
得分: 105
你必须使用 "\n"
。
在\n
上进行拆分,会在文本中搜索实际的\
后跟n
,而不是换行字节。
英文:
You have to use "\n"
.
Splitting on `\n`
, searches for an actual \
followed by n
in the text, not the newline byte.
答案2
得分: 44
对于我们有时使用 Windows 平台的人来说,可以在使用 split 之前使用 replace 来帮助记住:
strings.Split(strings.ReplaceAll(windows, "\r\n", "\n"), "\n")
英文:
For those of us that at times use Windows platform, it can
help remember to use replace before split:
strings.Split(strings.ReplaceAll(windows, "\r\n", "\n"), "\n")
答案3
得分: 20
这段代码无法正常工作是因为你使用了反引号:
原始字符串字面值是反引号``之间的字符序列。在引号内,除了反引号之外,任何字符都是合法的。原始字符串字面值的值是由引号之间的未解释(隐式UTF-8编码)字符组成的字符串;特别地,反斜杠没有特殊含义,字符串可以包含换行符。
参考:http://golang.org/ref/spec#String_literals
所以,当你执行
strings.Split(result,`\n`)
实际上是使用了两个连续的字符"\"和"n"进行分割,而不是换行符"\n"。要实现你想要的效果,只需使用"\n"
而不是反引号。
英文:
It does not work because you're using backticks:
> Raw string literals are character sequences between back quotes ``. Within the quotes, any character is legal except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines.
Reference: http://golang.org/ref/spec#String_literals
So, when you're doing
strings.Split(result,`\n`)
you're actually splitting using the two consecutive characters "" and "n", and not the character of line return "\n". To do what you want, simply use "\n"
instead of backticks.
答案4
得分: 12
你的代码不起作用是因为你使用了反引号而不是双引号。然而,如果你想支持Windows,你应该使用bufio.Scanner
。
import (
"bufio"
"strings"
)
func SplitLines(s string) []string {
var lines []string
sc := bufio.NewScanner(strings.NewReader(s))
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines
}
或者,你可以使用strings.FieldsFunc
(这种方法会跳过空行):
strings.FieldsFunc(s, func(c rune) bool { return c == '\n' || c == '\r' })
英文:
Your code doesn't work because you're using backticks instead of double quotes. However, you should be using a bufio.Scanner
if you want to support Windows.
import (
"bufio"
"strings"
)
func SplitLines(s string) []string {
var lines []string
sc := bufio.NewScanner(strings.NewReader(s))
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines
}
Alternatively, you can use strings.FieldsFunc
(this approach skips blank lines)
strings.FieldsFunc(s, func(c rune) bool { return c == '\n' || c == '\r' })
答案5
得分: 2
import regexp
var lines []string = regexp.MustCompile("\r?\n").Split(inputString, -1)
MustCompile()
创建一个正则表达式,可以通过\r\n
和\n
进行分割
Split()
执行分割,第二个参数设置最大分割部分的数量,-1
表示无限制。
英文:
import regexp
var lines []string = regexp.MustCompile("\r?\n").Split(inputString, -1)
MustCompile()
creates a regular expression that allows to split by both \r\n
and \n
Split()
performs the split, seconds argument sets maximum number of parts, -1
for unlimited
答案6
得分: -2
<kbd>'</kbd>
不起作用,因为它不是字符串类型,而是一个rune类型。
temp := strings.Split(result, '\n')
Go编译器报错:无法将'\u000a'(类型为rune)作为strings.Split的参数类型string
定义:Split(s, sep string) []string
英文:
<kbd>'</kbd> doesn't work because it is not a string type, but instead a rune.
temp := strings.Split(result,'\n')
go compiler: cannot use '\u000a' (type rune) as type string in argument to strings.Split
definition: Split(s, sep string) []string
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论