1. package main
    2. import (
    3. "fmt"
    4. "strings"
    5. )
    6. func testIndex() {
    7. str := "http://www.baidu.com"
    8. //查询字符出现的位置
    9. index := strings.Index(str,"baidu")
    10. fmt.Printf("baidu is index:%d\n",index)
    11. index = strings.LastIndex(str,"com")
    12. fmt.Printf("com is index:%d\n",index)
    13. }
    14. func main() {
    15. testIndex()
    16. }

    输出:

    1. baidu is index:11
    2. com is index:17