dongxietao0263 2019-05-04 17:07
浏览 352
已采纳

根据空格拆分子字符串golang数量有限

I need to combine strings.SplitN and strings.SplitFunc. the input string might have any kind of whitespace and I just need the first two splitted substrings and remaining string should be parsed in another run. The SplitN function splits the input up to the N parameter but just consider space not whitespace. You can define your special splitter in SplitFunc, in my case something like this:

f := func(r rune) bool {
    return unicode.IsSpace(r)
}

but I can not define the number of substring in SplitFunc.

the function strings.Fields is also not useful since there is no limit in this function.

  • 写回答

3条回答 默认 最新

  • duanhemou9834 2019-05-05 07:52
    关注

    regexp Split appears to do what you require

    s := regexp.MustCompile("[[:space:]]+")
    r:=s.Split("the     final conflict.  Of Cheese", 3)
        fmt.Println("first two ",r[:2])
        fmt.Println("remaining ",r[2:])
    }
    

    https://play.golang.org/p/-uoIljx0fKN

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题