I know that for counting the occurrence of one substring I can use "strings.Count(, )". What if I want to count the number of occurrences of substring1 OR substring2? Is there a more elegant way than writing another new line with strings.count()?
关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率
douba3943
2017-03-10 19:16浏览 1.1k
已采纳
Golang:要计算Go中某个字符串中一个或多个子字符串的出现?
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议
感谢您的意见,我们尽快改进~
我知道,为了计算一个子字符串的出现,我可以使用“ strings.Count(,)”。 如果我想计算substring1或substring2的出现次数怎么办? 是否有比用string.count()写另一行更优雅的方法? p> div>
- 写回答
- 好问题 提建议
- 追加酬金
- 关注问题
微信扫一扫
分享- 邀请回答
2条回答 默认 最新
- 关注
码龄 粉丝数 原力等级 --
- 被采纳
- 被点赞
- 采纳率
dsdf64562672 2017-03-10 19:20最佳回答 专家已采纳Use a regular expression:
https://play.golang.org/p/xMsHIYKtkQ
aORb := regexp.MustCompile("A|B") matches := aORb.FindAllStringIndex("A B C B A", -1) fmt.Println(len(matches))
采纳该答案 已采纳该答案 专家已采纳评论解决 无用打赏举报微信扫一扫
分享评论登录 后可回复...
查看更多回答(1条)
报告相同问题?
提交
相关推荐 更多相似问题
- 2017-03-10 19:16回答 2 已采纳 Use a regular expression: https://play.golang.org/p/xMsHIYKtkQ aORb := regexp.MustCompile("A|B")
- 2019-07-12 09:33回答 4 已采纳 How to remove the last letter from the string? In Go, character strings are UTF-8 encoded.
- 2016-06-07 20:13回答 3 已采纳 This is happening because chars is actually a string and is immutable. If you declared it appropri
- 2021-05-23 21:41浅弋、璃鱼的博客 在TCP/IP协议中, "IP地址+TCP或UDP端口号"唯一标识网络通讯中的一个进程; "IP地址+端口号"就对应一个socket; 欲建立连接的两个进程各自有一个socket来标识, 那么这两个socket组成的socket pair就唯一标识一个连接; ...
- 2022-05-10 18:36无休止符的博客 创建go程对应每一个客户端进行数据通信:go HandlerConnet() ⑤.实现HandlerConnet(conn net.Conn) a)defer conn.Close() b)获取成功连接的客户端 Addr:conn.RemoteAddr() c)for 循环 读取 客户端发送数据:conn...
- 2022-04-13 21:33IT_ziliang的博客 1、//从一个nil通道读取报fatal error: all goroutines are asleep - deadlock! func main1() { //var receiveChan <-chan interface{} //var sendChan chan<- interface{} //dataStream := make(chan ...
- 2018-04-02 11:39回答 1 已采纳 The Solution is sentence := fmt.Sprintf("My Name is %s", name)
- 2016-08-25 15:55回答 2 已采纳 Use a format verb that shows the data structure, like %#v. In this case %q works well too because
- 2019-02-26 23:27回答 1 已采纳 You may use ReplaceAllStringFunc and use a regex like (?m)^bar:(?: \s{4}.*)+ See the regex demo
- 2018-05-27 18:34医麦的博客 go语言已经做好了: 22.2.2 TCP的C/S架构 CS模型 :客户端(Client)和服务器(Server) 客户端——>客户 1)主动请求服务 服务器——>客服 2)被动提供服务 BS模型 :浏览器(Browser)和服务器(Server) ...
- 2018-06-14 11:52weixin_34178244的博客 编写一个 Go 程序可以逆转字符串,例如 “foobar” 被打印成 “raboof”。 Q4. (1) 平均值 编写计算一个类型是 float64 的 slice 的平均值的代码。在稍候的练习 Q5 中 将会改写为函数。 Q5. (0) 平均值...
- 2020-05-21 21:29weigh_的博客 并发是编程里面一个非常重要的概念,Go语言在语言层面天生支持并发,这也是Go语言流行的一个很重要的原因。Go语言的并发通过goroutine实现。goroutine类似于线程,属于用户态的线程,我们可以根据需要创建成千上万个...
- 2019-02-01 08:37回答 1 已采纳 Whenever performance and allocations are the question, you should run benchmarks. First let's mod
- 2014-05-25 15:59回答 3 已采纳 Using a map and 2 loops : play func check(s string) string { m := make(map[rune]uint, len(s))
- 2019-04-17 02:09回答 2 已采纳 Let me try to understand your problem, In the given input string, you want to find editor and send
- 2020-01-04 15:56hello_bravo_的博客 看了gc,发现没有内存的知识,光看gc,只能背个流程,其中很多涉及内存的操作,所以先来了解一波内存,然后了解gc的协程,golang的锁,再回头看gc
- 2022-04-16 18:24凉城未暖夜微凉的博客 Golang笔记数据类型基本数据类型转stringstring转基本数据类型数组切片mapmap常见操作标识符命名规则标识符命名规范go运算符优先级go流程控制条件语句选择语句循环语句标签、跳转语句延迟语句函数内置函数init函数...
- 2019-08-12 20:47小诚信驿站的博客 读写数据 ...go语言中是从键盘和标准输入os.Stdin读取输入,最简单的办法是使用fmt包提供的Scan和Sscan开头的函数。 Scanln 扫描来自标准输入的文本,将空格分隔的值依次存放到后续的参数内,直到碰到...
- 2017-05-17 06:43回答 2 已采纳 If you import strings package with different name then it will cause issue as it is used by the wr
- 2022-02-12 17:24itzhuzhu.的博客 如果端口号被另外一个服务或应用所占用,会导致当前程序启动失败 协议: 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定...
- 没有解决我的问题, 去提问