How to get number of results in strings.Contains?

go

Solution

Use

strings.Count(string, substring)

Count counts the number of non-overlapping instances of substring in string.

To use your example:

msg := "Lorem ipsum example of lorem ipsum."
fmt.Printf("contains %d occurences of ipsum", strings.Count(msg, "ipsum"))

Problem

When you want to check if a string contains some substring, you do it this way: ``` msg = "Lorem ipsum example of lorem ipsum." if strings.Contains(msg, "ipsum") { fmt.Println("contains word ipsum") } ``` How can I find out how many times the word "ipsum" appears in msg?

Original source