string 如何在 Golang 中查找字符索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20827332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:12:04  来源:igfitidea点击:

How to find a character index in Golang?

stringgocharacter

提问by mobu

I'm trying to find "@" string character in Go but I cannot find a way to do it. I know how to index characters like "HELLO[1]" which would output "E". However I'm trying to find index number of the found char.

我试图在 Go 中找到“@”字符串字符,但我找不到办法。我知道如何索引像“HELLO[1]”这样会输出“E”的字符。但是我试图找到找到的字符的索引号。

In Python I'd do it in following way:

在 Python 中,我会按照以下方式进行:

x = "chars@arefun"
split = x.find("@")
chars = x[:split]
arefun = x[split+1:]

>>>print split
5
>>>print chars
chars
>>>print arefun
arefun

So chars would return "chars" and arefun would return "arefun" while using "@" delimeter. I've been trying to find solution for hours and I cannot seem to find proper way to do it in Golang.

因此,在使用“@”分隔符时,chars 将返回“chars”,而 arefun 将返回“arefun”。我一直在努力寻找解决方案几个小时,但我似乎无法在 Golang 中找到正确的方法。

回答by siritinga

You can use the Indexfunction of package strings

您可以使用包的索引功能strings

Playground: https://play.golang.org/p/_WaIKDWCec

游乐场:https: //play.golang.org/p/_WaIKDWCec

package main

import "fmt"
import "strings"

func main() {
    x := "chars@arefun"

    i := strings.Index(x, "@")
    fmt.Println("Index: ", i)
    if i > -1 {
        chars := x[:i]
        arefun := x[i+1:]
        fmt.Println(chars)
        fmt.Println(arefun)
    } else {
        fmt.Println("Index not found")
        fmt.Println(x)
    }
}

回答by Xeoncross

If you are searching for non-ASCII characters (languages other than english) you need to use http://golang.org/x/text/search.

如果您要搜索非 ASCII 字符(英语以外的语言),则需要使用http://golang.org/x/text/search

func SubstringIndex(str string, substr string) int, bool {
    m := search.New(language.English, search.IgnoreCase)
    start, _ := m.IndexString(str, substr)
    if start == -1 {
        return start, false
    }
    return start, true
}

index, found := SearchForStringIndex('Aarhus', '?');
if found {
    fmt.Println("match starts at", index);
}

Search the language.Tagstructs hereto find the language you wish to search with or use language.Undif you are not sure.

如果您不确定,请language.Tag在此处搜索结构以查找您希望搜索或使用的语言language.Und

回答by JEX

Hello I am myself Python developer and tried solving your problem in Golang. I think its pretty readable if you have any questions let me know.

您好,我自己是 Python 开发人员,并尝试在 Golang 中解决您的问题。如果您有任何问题,请告诉我,我认为它的可读性很强。

var text = "FUN@WITH@GO@LANG@"

// ANSWER 1: 
func findTheSymbol(original string, desiredOne string) []int {
    var foundIndexes []int
    for i := 0; i < len(original); i++ {
        if desiredOne == string(original[i]) {
            foundIndexes = append(foundIndexes, i)
        }
    }
    return foundIndexes
}

// ANSWER 2: using Slice
func usingSlice(original string, desiredOne string) []string {
    return strings.Split(text, "@")
}

func main() {
    // Found Indexes
    ind := findTheSymbol(text, "@")
    // checking length of array/slice
    if len(ind) > 0 {
        var startIndex int = 0
        fmt.Println("The Index/s: ", ind)
        for i := 0; i < len(ind); i++ {
            fmt.Println("part = ", i, string(text[startIndex:ind[i]]))
            startIndex = ind[i] + 1
        }
    } else {
        fmt.Println("Unable to find: ", ind)
    }

    // use the slice
    fmt.Println(strings.Join(usingSlice(text, "@"), ", "))
}

Running example here: https://repl.it/repls/ExcitedAgreeableAlgorithm

在此处运行示例:https: //repl.it/repls/ExcitedAgreeableAlgorithm