Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何在字符串中的特定位置查找精确字符_Go - Fatal编程技术网

Go 如何在字符串中的特定位置查找精确字符

Go 如何在字符串中的特定位置查找精确字符,go,Go,我有以下字符串: absoloute-power 如您所见,字符串中的位置10处有一个“-”。如何编写go代码来验证任何给定字符串的第10个位置是否在字符串中有“-”?您可以使用符文数组: text := "ifthisisyourstring" chars := []rune(text) if chars[0] == '1' { // is true } 在Go中,字符串值是以UTF-8编码的Unicode字符。UTF-8是一种可变长度编码,每个字符使用一到四个字节 例如: pac

我有以下字符串:

absoloute-power

如您所见,字符串中的位置10处有一个“-”。如何编写go代码来验证任何给定字符串的第10个位置是否在字符串中有“-”?

您可以使用符文数组:

text := "ifthisisyourstring"
chars := []rune(text)
if chars[0] == '1' {
    // is true
}

在Go中,字符串值是以UTF-8编码的Unicode字符。UTF-8是一种可变长度编码,每个字符使用一到四个字节

例如:

package main

import (
    "fmt"
    "unicode/utf8"
)

func is10Hyphen(s string) bool {
    for n := 1; len(s) > 0; n++ {
        r, size := utf8.DecodeRuneInString(s)
        if r == utf8.RuneError && (size == 0 || size == 1) {
            return false
        }
        if n == 10 {
            return r == '-'
        }
        s = s[size:]
    }
    return false
}

func main() {
    s := "absoloute-power"
    fmt.Println(is10Hyphen(s))
    s = "absoloute+power"
    fmt.Println(is10Hyphen(s))
    s = "absoloute"
    fmt.Println(is10Hyphen(s))
}
func is10Hyphen(s string) bool {
    n := 0
    for _, r := range s {
        if r == utf8.RuneError {
            return false
        }
        n++
        if n == 10 {
            return r == '-'
        }
    }
    return false
}
游乐场:

输出:

true
false
false

如果您愿意考虑遇到Unicode替换字符的错误,那么您的例子是:

package main

import (
    "fmt"
    "unicode/utf8"
)

func is10Hyphen(s string) bool {
    for n := 1; len(s) > 0; n++ {
        r, size := utf8.DecodeRuneInString(s)
        if r == utf8.RuneError && (size == 0 || size == 1) {
            return false
        }
        if n == 10 {
            return r == '-'
        }
        s = s[size:]
    }
    return false
}

func main() {
    s := "absoloute-power"
    fmt.Println(is10Hyphen(s))
    s = "absoloute+power"
    fmt.Println(is10Hyphen(s))
    s = "absoloute"
    fmt.Println(is10Hyphen(s))
}
func is10Hyphen(s string) bool {
    n := 0
    for _, r := range s {
        if r == utf8.RuneError {
            return false
        }
        n++
        if n == 10 {
            return r == '-'
        }
    }
    return false
}

操场:

由于字符串实际上是一个数组,您可以直接访问第10个位置。当然,需要避免“超出范围”错误。 对于非ascii编码,将其转换为符文数组

package main

import (
  "fmt"
)

func main() {
  fmt.Println(Check("213123-dasdas"))
  fmt.Println(Check("213123sdas"))
  fmt.Println(Check("213123das-das"))
  fmt.Println(Check("213123dasda-s"))
  fmt.Println(Check("---------2----------"))
}

func Check(ss string) bool {
  r = []rune(ss)
  if len(r) < 10 {
    return false
  }
  return ss[r] == '-'
}
主程序包
进口(
“fmt”
)
func main(){
fmt.Println(支票(“213123 dasdas”))
fmt.Println(支票(“213123sdas”))
fmt.Println(检查(“213123das”))
fmt.Println(检查(“213123dasda-s”))
格式打印项次(勾选(“-----------2-----------”)
}
功能检查(ss字符串)布尔{
r=[]符文(ss)
如果len(r)<10{
返回错误
}
返回ss[r]='-'
}

您尝试过什么?请给出示例代码。这个网站是为了解决编程问题,而不是“为我写简单的代码”。从这里开始-可能的副本,在play.golang.org中,您可以测试/分享您的经历doing@nbari:您的代码()不执行切片边界检查。它可能
死机:运行时错误:索引超出范围
。您的代码不适用于“任何给定字符串”。它只适用于ASCII字符。您的代码不进行切片边界检查。它可能
死机:运行时错误:索引超出范围
。将整个字符串转换为符文片段的效率很低。您的代码不适用于“任何给定字符串”。它只适用于ASCII字符。是的,我忘记了使用utf8.DecodeRuneInString处理无效编码的情况?