How to compare strings in golang?

huangapple go评论129阅读模式
英文:

How to compare strings in golang?

问题

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

  1. foo:="Makan"
  2. bar:="Makon"

结果应该是3。

  1. foo:="Indah"
  2. bar:="Ihkasyandehlo"

结果应该是1。

英文:

I want to make a function that calculates the length of the common segment (starting from the beginning) in two strings. For example:

  1. foo:="Makan"
  2. bar:="Makon"

The result should be 3.

  1. foo:="Indah"
  2. bar:="Ihkasyandehlo"

The result should be 1.

答案1

得分: 4

这是一个用Go语言编写的程序,它包含了三个函数:commonBytes、commonRunes和commonBytesRunes。这些函数用于比较两个字符串中相同的字节和符文的数量。

在程序中,有一个包含了三个测试用例的Tests切片。每个测试用例都包含两个字符串word1和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

你可以在这里查看完整的代码。

英文:

It's not clear what you are asking because you limited your test cases to ASCII characters.
I've added a Unicode test case and I've included answers for bytes, runes, or both.

<kbd>play.golang.org</kbd>:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;unicode/utf8&quot;
  5. )
  6. func commonBytes(s, t string) (bytes int) {
  7. if len(s) &gt; len(t) {
  8. s, t = t, s
  9. }
  10. i := 0
  11. for ; i &lt; len(s); i++ {
  12. if s[i] != t[i] {
  13. break
  14. }
  15. }
  16. return i
  17. }
  18. func commonRunes(s, t string) (runes int) {
  19. if len(s) &gt; len(t) {
  20. s, t = t, s
  21. }
  22. i := 0
  23. for ; i &lt; len(s); i++ {
  24. if s[i] != t[i] {
  25. break
  26. }
  27. }
  28. return utf8.RuneCountInString(s[:i])
  29. }
  30. func commonBytesRunes(s, t string) (bytes, runes int) {
  31. if len(s) &gt; len(t) {
  32. s, t = t, s
  33. }
  34. i := 0
  35. for ; i &lt; len(s); i++ {
  36. if s[i] != t[i] {
  37. break
  38. }
  39. }
  40. return i, utf8.RuneCountInString(s[:i])
  41. }
  42. func main() {
  43. Tests := []struct {
  44. word1, word2 string
  45. }{
  46. {&quot;Makan&quot;, &quot;Makon&quot;},
  47. {&quot;Indah&quot;, &quot;Ihkasyandehlo&quot;},
  48. {&quot;日本語&quot;, &quot;日本語&quot;},
  49. }
  50. for _, test := range Tests {
  51. fmt.Println(&quot;Words: &quot;, test.word1, test.word2)
  52. fmt.Println(&quot;Bytes: &quot;, commonBytes(test.word1, test.word2))
  53. fmt.Println(&quot;Runes: &quot;, commonRunes(test.word1, test.word2))
  54. fmt.Print(&quot;Bytes &amp; Runes: &quot;)
  55. fmt.Println(commonBytesRunes(test.word1, test.word2))
  56. }
  57. }

Output:

<pre>
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
</pre>

答案2

得分: 3

请注意,如果您使用的是 Unicode 字符,结果可能会有很大不同。
例如,尝试使用 utf8.DecodeRuneInString()

参考这个示例

  1. package main
  2. import "fmt"
  3. import "unicode/utf8"
  4. func index(s1, s2 string) int {
  5. res := 0
  6. for i, w := 0, 0; i < len(s2); i += w {
  7. if i >= len(s1) {
  8. return res
  9. }
  10. runeValue1, width := utf8.DecodeRuneInString(s1[i:])
  11. runeValue2, width := utf8.DecodeRuneInString(s2[i:])
  12. if runeValue1 != runeValue2 {
  13. return res
  14. }
  15. if runeValue1 == utf8.RuneError || runeValue2 == utf8.RuneError {
  16. return res
  17. }
  18. w = width
  19. res = i + w
  20. }
  21. return res
  22. }
  23. func main() {
  24. foo := "日本本a語"
  25. bar := "日本本b語"
  26. fmt.Println(index(foo, bar))
  27. foo = "日本語"
  28. bar = "日otest"
  29. fmt.Println(index(foo, bar))
  30. foo = "\xF0"
  31. bar = "\xFF"
  32. fmt.Println(index(foo, bar))
  33. }

在这个例子中,结果将是:

  • 9(3个宽度为'3'的公共符文)

  • 3(1个宽度为'3'的符文)

  • 0(无效符文,表示 utf8.RuneError

英文:

Note that if you were working with Unicode characters, the result could be quite different.
Try for instance using utf8.DecodeRuneInString().

See this example:

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;unicode/utf8&quot;
  4. func index(s1, s2 string) int {
  5. res := 0
  6. for i, w := 0, 0; i &lt; len(s2); i += w {
  7. if i &gt;= len(s1) {
  8. return res
  9. }
  10. runeValue1, width := utf8.DecodeRuneInString(s1[i:])
  11. runeValue2, width := utf8.DecodeRuneInString(s2[i:])
  12. if runeValue1 != runeValue2 {
  13. return res
  14. }
  15. if runeValue1 == utf8.RuneError || runeValue2 == utf8.RuneError {
  16. return res
  17. }
  18. w = width
  19. res = i + w
  20. }
  21. return res
  22. }
  23. func main() {
  24. foo := &quot;日本本a語&quot;
  25. bar := &quot;日本本b語&quot;
  26. fmt.Println(index(foo, bar))
  27. foo = &quot;日本語&quot;
  28. bar = &quot;日otest&quot;
  29. fmt.Println(index(foo, bar))
  30. foo = &quot;\xF0&quot;
  31. bar = &quot;\xFF&quot;
  32. fmt.Println(index(foo, bar))
  33. }

Here, the result would be:

  • 9 (3 common runes of width '3')

  • 3 (1 rune of width '3')

  • 0 (invalid rune, meaning utf8.RuneError)

答案3

得分: 2

你是说像这样的。请注意,这段代码只能处理ASCII字符,无法处理UTF-8字符。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func equal(s1, s2 string) int {
  6. eq := 0
  7. if len(s1) > len(s2) {
  8. s1, s2 = s2, s1
  9. }
  10. for key, _ := range s1 {
  11. if s1[key] == s2[key] {
  12. eq++
  13. } else {
  14. break
  15. }
  16. }
  17. return eq
  18. }
  19. func main() {
  20. fmt.Println(equal("buzzfizz", "buzz"))
  21. fmt.Println(equal("Makan", "Makon"))
  22. fmt.Println(equal("Indah", "Ihkasyandehlo"))
  23. }

这段代码定义了一个名为equal的函数,用于比较两个字符串的相等字符数。在main函数中,我们调用了equal函数来比较不同的字符串。

英文:

You mean like this. Please note, this will not handle UTF 8, only ascii.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func equal(s1, s2 string) int {
  6. eq := 0
  7. if len(s1) &gt; len(s2) {
  8. s1, s2 = s2, s1
  9. }
  10. for key, _ := range s1 {
  11. if s1[key] == s2[key] {
  12. eq++
  13. } else {
  14. break
  15. }
  16. }
  17. return eq
  18. }
  19. func main() {
  20. fmt.Println(equal(&quot;buzzfizz&quot;, &quot;buzz&quot;))
  21. fmt.Println(equal(&quot;Makan&quot;, &quot;Makon&quot;))
  22. fmt.Println(equal(&quot;Indah&quot;, &quot;Ihkasyandehlo&quot;))
  23. }

huangapple
  • 本文由 发表于 2014年11月22日 17:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/27076044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定