2014-11-22 120 views
-9

我想製作一個函數,用兩個字符串計算公共段(從頭開始)的長度。例如:如何比較golang中的字符串?

foo:="Makan" 
bar:="Makon" 

結果應該是3

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

結果應該是1

+0

這甚至不是一個問題。 – Topo 2014-11-22 10:07:05

+1

這就像編程101課的作業或類似的東西。如果你不知道如何編寫一些基本的東西,那麼你應該真的在做你的功課,而不是要求某人爲你做。 – Topo 2014-11-22 10:10:13

+1

這是我的問題來解決我的功課,我的作業是(「如何使一個簡單的帕特里夏特里」),我只是困惑時,我想檢查根密鑰與新的關鍵..... – 2014-11-22 10:13:30

回答

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")) 
} 
2

請注意,如果您使用的是Unicode字符,結果可能會大不相同。
嘗試使用例如utf8.DecodeRuneInString()

參見this example

package main 

import "fmt" 
import "unicode/utf8" 

func index(s1, s2 string) int { 
    res := 0 
    for i, w := 0, 0; i < len(s2); i += w { 
     if i >= len(s1) { 
      return res 
     } 
     runeValue1, width := utf8.DecodeRuneInString(s1[i:]) 
     runeValue2, width := utf8.DecodeRuneInString(s2[i:]) 
     if runeValue1 != runeValue2 { 
      return res 
     } 
     if runeValue1 == utf8.RuneError || runeValue2 == utf8.RuneError { 
      return res 
     } 
     w = width 
     res = i + w 
    } 
    return res 
} 

func main() { 
    foo := "日本本a語" 
    bar := "日本本b語" 
    fmt.Println(index(foo, bar)) 
    foo = "日本語" 
    bar = "日otest" 
    fmt.Println(index(foo, bar)) 
    foo = "\xF0" 
    bar = "\xFF" 
    fmt.Println(index(foo, bar)) 
} 

在此,其結果將是:

  • 9(3個的寬度 '3' 共同符)
  • 3(1符的寬度 '3' )
  • 0(無效符文,意思是utf8.RuneError
+0

不要忽略錯誤。例如,'foo =「\ xF0」; bar =「\ xFF」; fmt.Println(index(foo,bar))'打印'1'而不是'0':http://play.golang.org/p/osAFlIubxZ。 – peterSO 2014-11-23 20:18:09

+0

@peterSO的確如此。我錯過了'utf8.RuneError'的情況。 – VonC 2014-11-24 12:14:31

3

由於您將測試用例限制爲ASCII字符,因此您不清楚要問什麼。
我已經添加了一個Unicode測試用例,並且包含字節,符文或兩者的答案。

play.golang.org

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