2.1 字符串查询

package main

import (
	"fmt"
	"strings"
)

const refString = "Mary had a little lamb"

func main() {

	lookFor := "lamb"
	contain := strings.Contains(refString, lookFor)
	fmt.Printf("The \"%s\" contains \"%s\": %t \n", refString, lookFor, contain)

	lookFor = "wolf"
	contain = strings.Contains(refString, lookFor)
	fmt.Printf("The \"%s\" contains \"%s\": %t \n", refString, lookFor, contain)

	startsWith := "Mary"
	starts := strings.HasPrefix(refString, startsWith)
	fmt.Printf("The \"%s\" starts with \"%s\": %t \n", refString, startsWith, starts)

	endWith := "lamb"
	ends := strings.HasSuffix(refString, endWith)
	fmt.Printf("The \"%s\" ends with \"%s\": %t \n", refString, endWith, ends)

}

/*
The "Mary had a little lamb" contains "lamb": true
The "Mary had a little lamb" contains "wolf": false
The "Mary had a little lamb" starts with "Mary": true
The "Mary had a little lamb" ends with "lamb": true
*/

posted on 2018-03-21 22:22  cucy_to  阅读(100)  评论(0编辑  收藏  举报

导航