很经常需要对字符串进行解析,解析中间大多是使用空格或者TAB或者制表符进行分割的。但是具体多少个空格很难量化。
在Python语言中我们只需要不带参数就可以轻松分割,比如:
a = ' a b c d e'
print(a.split())
//get return [a, b, c, d, e]
最近在golang里面也是碰到了这样的问题,但是使用方式貌似不太对
错误用法:
错误用法1:
a := "a b c d"
res := strings.Split(a, " ")
fmt.Println(res)
//get the result [a, , b, , , c, d]
错误用法2:
a := "a b c d"
res := strings.Split(a, "")
fmt.Println(res)
//get the result [a, , ,b, , , ,c, ,d]
自习看了下API文档没有找到什么简单的语法糖或者方法。所以这边最后还是使用正则表达式来处理。
import "regexp"
func main(){
a := "a b c d e"
r := regexp.MustCompile("[^\\s]+")
res:= r.FindAllString(a, -1)
fmt.Println(res)
}