go 的字符查看位置

多个fmt.Scanf()输入方式:

Please Input Str: Please Input Substr: str=asdfsadfadsafdfddsfsad,substr=d
输出:2 21

点击查看代码
package main

import (
	"fmt"
	"strings"
)

func StrIndex(str string, substr string) (int, int) {
	fmt.Printf("str=%s,substr=%s \n",str,substr)
	return strings.Index(str, substr), strings.LastIndex(str, substr)
}

func main() {
	var (
		str string
		substr string
	)
	fmt.Printf("Please Input Str: ")
	fmt.Scanf("%s", &str)
	fmt.Printf("Please Input Substr: ")
	fmt.Scanf("%s", &substr)
	fmt.Println(StrIndex(str, substr))
}


posted @ 2022-02-27 13:16  ty1539  阅读(159)  评论(0编辑  收藏  举报