duangao8359 2017-01-31 11:26
浏览 146
已采纳

如何使用go在符文中找到字符串的偏移量索引

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.

  • 写回答

1条回答 默认 最新

  • dougu3591 2017-01-31 11:44
    关注

    Edit #2: You again indicated a new type "meaning" of your question: you want to search a string in a []rune.

    Answer: this is not supported directly in the standard library. But it's easy to implement it with 2 for loops:

    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
    }
    

    Testing it:

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

    Output (try it on the Go Playground):

    7
    

    Edit: You updated the question indicating that you want to search runes in a string.

    You may easily convert a []rune to a string using a simple type conversion:

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

    And from there on, you can use strings.Index() as you did in your example:

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

    Try it on the Go Playground.

    Original answer follows:


    string values in Go are stored as UTF-8 encoded bytes. strings.Index() returns you the byte position if the given substring is found.

    So basically what you want is to convert this byte-position to rune-position. The unicode/utf8 package contains utility functions for telling the rune-count or rune-length of a string: utf8.RuneCountInString().

    So basically you just need to pass the substring to this function:

    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]))
    }
    

    Output (try it on the Go Playground):

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

    Note: offset must also be a byte position, because when slicing a string like text[offset:], the index is interpreted as byte-index.

    If you want to get the index of a rune, use strings.IndexRune() instead of strings.Index().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?