英文:
Split a string on whitespace in Go?
问题
给定一个输入字符串,例如" word1 word2 word3 word4 "
,在Go中将其拆分为字符串数组的最佳方法是什么?请注意,每个单词之间可以有任意数量的空格或Unicode空格字符。
在Java中,我只需使用someString.trim().split("\\s+")
。
(注意:可能是重复的https://stackoverflow.com/questions/4466091/split-string-using-regular-expression-in-go没有给出任何好的答案。请提供一个实际的示例,而不仅仅是对regexp
或strings
包的引用链接。)
英文:
Given an input string such as " word1 word2 word3 word4 "
, what would be the best approach to split this as an array of strings in Go? Note that there can be any number of spaces or unicode-spacing characters between each word.
In Java I would just use someString.trim().split("\\s+")
.
(Note: possible duplicate https://stackoverflow.com/questions/4466091/split-string-using-regular-expression-in-go doesn't give any good quality answer. Please provide an actual example, not just a link to the regexp
or strings
packages reference.)
答案1
得分: 380
strings
包有一个Fields
方法。
someString := "one two three four "
words := strings.Fields(someString)
fmt.Println(words, len(words)) // [one two three four] 4
DEMO: http://play.golang.org/p/et97S90cIH
从文档中可以看到:
> Fields方法根据unicode.IsSpace定义的一个或多个连续的空白字符将字符串s分割成子字符串的切片,如果s只包含空白字符,则返回一个空切片。
英文:
The strings
package has a Fields
method.
someString := "one two three four "
words := strings.Fields(someString)
fmt.Println(words, len(words)) // [one two three four] 4
DEMO: http://play.golang.org/p/et97S90cIH
From the docs:
> Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
答案2
得分: 12
如果您正在使用tip: regexp.Split
func (re *Regexp) Split(s string, n int) []string
Split函数将字符串s切割成由表达式分隔的子字符串,并返回这些表达式匹配之间的子字符串的切片。
此方法返回的切片由s中不包含在FindAllString返回的切片中的所有子字符串组成。当应用于不包含元字符的表达式时,它等效于strings.SplitN。
示例:
s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
// s: ["", "b", "b", "c", "cadaaae"]
count参数确定要返回的子字符串的数量:
n > 0: 最多返回n个子字符串;最后一个子字符串将是未切割的剩余部分。
n == 0: 结果为nil(零个子字符串)
n < 0: 所有子字符串
英文:
If you're using tip: regexp.Split
func (re *Regexp) Split(s string, n int) []string
Split slices s into substrings separated by the expression and returns
a slice of the substrings between those expression matches.
The slice returned by this method consists of all the substrings
of s not contained in the slice returned by FindAllString. When called
on an expression that contains no metacharacters, it is equivalent to strings.SplitN.
Example:
s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
// s: ["", "b", "b", "c", "cadaaae"]
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substrings
答案3
得分: 7
我想到了以下的解决方案,但是看起来有点冗长:
import "regexp"
r := regexp.MustCompile("[^\\s]+")
r.FindAllString(" word1 word2 word3 word4 ", -1)
这将返回:
[]string{"word1", "word2", "word3", "word4"}
是否有更简洁或更符合惯用表达的方式?
英文:
I came up with the following, but that seems a bit too verbose:
import "regexp"
r := regexp.MustCompile("[^\\s]+")
r.FindAllString(" word1 word2 word3 word4 ", -1)
which will evaluate to:
[]string{"word1", "word2", "word3", "word4"}
Is there a more compact or more idiomatic expression?
答案4
得分: 3
你可以使用strings包的split函数
strings.Split(某个字符串, " ")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论