-2

How found offset index a string in []rune using go?

I can do this work with string type.

if i := strings.Index(input[offset:], "}}"); i > 0 {print(i);}

but i need for runes.

i have a rune and want get offset index.

how can do this work with runes type in go?

example for more undrestand want need:

int offset=0//mean start from 0 (this is important for me)
string text="123456783}}56"
if i := strings.Index(text[offset:], "}}"); i > 0 {print(i);}

output of this example is : 9

but i want do this with []rune type(text variable)

may?

see my current code : https://play.golang.org/p/seImKzVpdh

tank you.

4

1 回答 1

3

编辑#2:您再次指出了问题的一种新类型“含义”:您想string在 a 中搜索 a []rune

答:标准库中不直接支持。for但是用 2 个循环很容易实现它:

func search(text []rune, what string) int {
    whatRunes := []rune(what)

    for i := range text {
        found := true
        for j := range whatRunes {
            if text[i+j] != whatRunes[j] {
                found = false
                break
            }
        }
        if found {
            return i
        }
    }
    return -1
}

测试它:

value := []rune("123}456}}789")
result := search(value, "}}")
fmt.Println(result)

输出(在Go Playground上试试):

7

编辑:您更新了表明您要在 a 中搜索runes 的问题string

您可以使用简单的类型转换轻松地将 a 转换[]rune为 a :string

toSearchRunes := []rune{'}', '}'}
toSearch := string(toSearchRunes)

从那里开始,您可以strings.Index()像在示例中那样使用:

if i := strings.Index(text[offset:], toSearch); i > 0 {
    print(i)
}

在Go Playground上尝试一下。

原答案如下:


stringGo 中的值存储为 UTF-8 编码字节。strings.Index()如果找到给定的子字符串,则返回字节位置。

所以基本上你想要的是将这个字节位置转换为符文位置。该unicode/utf8软件包包含用于告诉符文计数或符文长度的实用函数string: utf8.RuneCountInString()

所以基本上你只需要将子字符串传递给这个函数:

offset := 0
text := "123456789}}56"
if i := strings.Index(text[offset:], "}}"); i > 0 {
    fmt.Println("byte-pos:", i, "rune-pos:", utf8.RuneCountInString(text[offset:i]))
}

text = "世界}}世界"
if i := strings.Index(text[offset:], "}}"); i > 0 {
    fmt.Println("byte-pos:", i, "rune-pos:", utf8.RuneCountInString(text[offset:i]))
}

输出(在Go Playground上试试):

byte-pos: 9 rune-pos: 9
byte-pos: 6 rune-pos: 2

注意:offset也必须是字节位置,因为在对stringlike进行切片时text[offset:],索引被解释为字节索引。

如果要获取 a 的索引rune,请使用strings.IndexRune()而不是strings.Index().

于 2017-01-31T11:44:09.853 回答