在golang中如何切片带"的字符?

比方说我现在有个字符串是

<a class="pturl" href="/24711161/plain/">Download as text</a>

很显然,这是个html中的元素

我可以这样切出来:

    index1 := strings.Index(str_body, "pturl")
    index2 := strings.Index(str_body, "Download as text")
    str := str_body[index1:index2]
    fmt.Println(str)//pturl" href="/24711161/plain/">

但是我想切出/24711161/plain/,于是尝试这么写:

    index1 := strings.Index(str_body, "pturl" href="")
    index2 := strings.Index(str_body, "Download as text")
    str := str_body[index1:index2]
    fmt.Println(str)//pturl" href="/24711161/plain/">

但是会报错: syntax error: unexpected href, expecting comma or )

请问我该怎么做?

阅读 3.2k
3 个回答

对Go不熟,感觉有点傻,求各位大神不喷哈:

    preStr := "pturl\" href=\""
    nextStr := "/\">Download as text"
    index1 := strings.Index(str_body, preStr)
    index2 := strings.Index(str_body, nextStr)
    preStrSize := len(preStr)
    str := str_body[index1+size : index2]
    fmt.Println(str)

没转义。

    index1 := strings.Index(str_body, "pturl\" href=\"")

没写过Go,我猜是这样的。

可以用正则的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题