在golang中如何切片带"的字符?
本文介绍了在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 )
请问我该怎么做?
解决方案
对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)
这篇关于在golang中如何切片带"的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文