美文网首页📗Go语言:基础大全
Go语言:获取中文子字符串的位置(原创、支持Unicode、最高

Go语言:获取中文子字符串的位置(原创、支持Unicode、最高

作者: 白祤星 | 来源:发表于2019-06-21 11:54 被阅读8441次

导言:


代码实例:


package main

func main() {
    println(Unicode_Index(`你好世界`, `世界`))
}

func Unicode_Index(str, substr string) int {
    pos := 0
    len := len(substr)
    for i, _ := range str {
        if str[i] == substr[0] {
            if str[i:i+len] == substr {
                return pos
            }
        }
        pos++
    }
    return -1
}

相关文章

网友评论

    本文标题:Go语言:获取中文子字符串的位置(原创、支持Unicode、最高

    本文链接:https://www.haomeiwen.com/subject/rshyqctx.html