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-26T10:12:25Z

Updated

2019-03-26T10:12:25Z

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "where hello is?"
	toFind := "hello"
	idx := strings.Index(s, toFind)
	fmt.Printf("'%s' is in s starting at position %d\n", toFind, idx)

	// when string is not found, result is -1
	idx = strings.Index(s, "not present")
	fmt.Printf("Index of non-existent substring is: %d\n", idx)
}
INFO