String 如何比较golang中的字符串?

String 如何比较golang中的字符串?,string,go,comparator,String,Go,Comparator,我想做一个函数,计算两个字符串中公共段的长度(从开头开始)。例如: foo:="Makan" bar:="Makon" 结果应该是3 foo:="Indah" bar:="Ihkasyandehlo" 结果应该是1。你的意思是这样的。请注意,这将不处理UTF 8,仅处理ascii package main import ( "fmt" ) func equal(s1, s2 string) int { eq := 0 if len(s1) > len(s2)

我想做一个函数,计算两个字符串中公共段的长度(从开头开始)。例如:

foo:="Makan"
bar:="Makon"
结果应该是3

foo:="Indah"
bar:="Ihkasyandehlo"

结果应该是1。

你的意思是这样的。请注意,这将不处理UTF 8,仅处理ascii

package main

import (
    "fmt"
)

func equal(s1, s2 string) int {
    eq := 0
    if len(s1) > len(s2) {
        s1, s2 = s2, s1
    }
    for key, _ := range s1 {
        if s1[key] == s2[key] {
            eq++
        } else {
            break
        }
    }
    return eq
}

func main() {
    fmt.Println(equal("buzzfizz", "buzz"))
    fmt.Println(equal("Makan", "Makon"))
    fmt.Println(equal("Indah", "Ihkasyandehlo"))
}

请注意,如果使用Unicode字符,结果可能会截然不同。
例如,尝试使用

见:

主程序包
输入“fmt”
导入“unicode/utf8”
func索引(s1,s2字符串)int{
res:=0
对于i,w:=0,0;i=len(s1){
返回res
}
runeValue1,宽度:=utf8.decodeRuneInstalling(s1[i:])
runeValue2,宽度:=utf8.decodeRuneInstalling(s2[i:])
如果runeValue1!=runeValue2{
返回res
}
如果runeValue1==utf8.RuneError | | runeValue2==utf8.RuneError{
返回res
}
w=宽度
res=i+w
}
返回res
}
func main(){
福:=”日本本A.語"
酒吧:=”日本本B語"
格式打印项次(索引(foo,bar))
福=”日本語"
巴=”日奥特斯特”
格式打印项次(索引(foo,bar))
foo=“\xF0”
bar=“\xFF”
格式打印项次(索引(foo,bar))
}
在这里,结果将是:

  • 9(3个宽度为“3”的普通符文)
  • 3(1个宽度为“3”的符文)
  • 0(无效符文,含义)

    • 不清楚您在问什么,因为您将测试用例限制为ASCII字符。
      我添加了一个Unicode测试用例,并包含了字节、符文或两者的答案

      :

      主程序包
      进口(
      “fmt”
      “unicode/utf8”
      )
      func commonBytes(s,t字符串)(字节整数){
      如果len(s)>len(t){
      s、 t=t,s
      }
      i:=0
      对于;ilen(t){
      s、 t=t,s
      }
      i:=0
      对于;ilen(t){
      s、 t=t,s
      }
      i:=0
      对于;i
      输出:

      Words: Makan Makon Bytes: 3 Runes: 3 Bytes & Runes: 3 3 Words: Indah Ihkasyandehlo Bytes: 1 Runes: 1 Bytes & Runes: 1 1 Words: 日本語 日本語 Bytes: 9 Runes: 3 Bytes & Runes: 9 3 单词:Makan Makon 字节:3 符文:3 字节和符文:3 文字:因达伊赫卡西亚德洛 字节:1 符文:1 字节和符文:1 话:日本語 日本語 字节:9 符文:3 字节和符文:9 3
      这甚至不是一个问题。这就像你的编程101课的家庭作业或类似的东西。如果你不知道如何编写如此基本的程序,你应该真正做你的家庭作业,而不是让别人帮你做。这是我解决家庭作业的问题,我的家庭作业是(“如何制作一个简单的patricia trie”),当我想用新键检查根键时,我只是混淆了…不要忽略错误。例如,
      foo=“\xF0”bar=“\xFF”fmt.Println(index(foo,bar))
      prints
      1
      而不是
      0
      :。@peterSO确实如此。我错过了
      utf8.RuneError
      案例。
      package main
      
      import (
          "fmt"
          "unicode/utf8"
      )
      
      func commonBytes(s, t string) (bytes int) {
          if len(s) > len(t) {
              s, t = t, s
          }
          i := 0
          for ; i < len(s); i++ {
              if s[i] != t[i] {
                  break
              }
          }
          return i
      }
      
      func commonRunes(s, t string) (runes int) {
          if len(s) > len(t) {
              s, t = t, s
          }
          i := 0
          for ; i < len(s); i++ {
              if s[i] != t[i] {
                  break
              }
          }
          return utf8.RuneCountInString(s[:i])
      }
      
      func commonBytesRunes(s, t string) (bytes, runes int) {
          if len(s) > len(t) {
              s, t = t, s
          }
          i := 0
          for ; i < len(s); i++ {
              if s[i] != t[i] {
                  break
              }
          }
          return i, utf8.RuneCountInString(s[:i])
      }
      
      func main() {
          Tests := []struct {
              word1, word2 string
          }{
              {"Makan", "Makon"},
              {"Indah", "Ihkasyandehlo"},
              {"日本語", "日本語"},
          }
          for _, test := range Tests {
              fmt.Println("Words:        ", test.word1, test.word2)
              fmt.Println("Bytes:        ", commonBytes(test.word1, test.word2))
              fmt.Println("Runes:        ", commonRunes(test.word1, test.word2))
              fmt.Print("Bytes & Runes: ")
              fmt.Println(commonBytesRunes(test.word1, test.word2))
          }
      }
      
      Words: Makan Makon Bytes: 3 Runes: 3 Bytes & Runes: 3 3 Words: Indah Ihkasyandehlo Bytes: 1 Runes: 1 Bytes & Runes: 1 1 Words: 日本語 日本語 Bytes: 9 Runes: 3 Bytes & Runes: 9 3