Quick actions

cmd+k|ctrl+k

Navigation

Languages

Strings / Find string in another string / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T09:38:53Z

Updated

2019-03-26T09:38:53Z

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "first is, second is, third is"
	toFind := "is"
	currStart := 0
	for {
		idx := strings.Index(s, toFind)
		if idx == -1 {
			break
		}
		fmt.Printf("found '%s' at position %d\n", toFind, currStart+idx)
		currStart += idx + len(toFind)
		s = s[idx+len(toFind):]
	}
}
INFO