How to compare strings in golang?

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

How to compare strings in golang?

问题

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

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

结果应该是3。

foo:="Indah"
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:

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

The result should be 3.

foo:="Indah"
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>:

package main

import (
	&quot;fmt&quot;
	&quot;unicode/utf8&quot;
)

func commonBytes(s, t string) (bytes int) {
	if len(s) &gt; len(t) {
		s, t = t, s
	}
	i := 0
	for ; i &lt; len(s); i++ {
		if s[i] != t[i] {
			break
		}
	}
	return i
}

func commonRunes(s, t string) (runes int) {
	if len(s) &gt; len(t) {
		s, t = t, s
	}
	i := 0
	for ; i &lt; 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) &gt; len(t) {
		s, t = t, s
	}
	i := 0
	for ; i &lt; len(s); i++ {
		if s[i] != t[i] {
			break
		}
	}
	return i, utf8.RuneCountInString(s[:i])
}

func main() {
	Tests := []struct {
		word1, word2 string
	}{
		{&quot;Makan&quot;, &quot;Makon&quot;},
		{&quot;Indah&quot;, &quot;Ihkasyandehlo&quot;},
		{&quot;日本語&quot;, &quot;日本語&quot;},
	}
	for _, test := range Tests {
		fmt.Println(&quot;Words:        &quot;, test.word1, test.word2)
		fmt.Println(&quot;Bytes:        &quot;, commonBytes(test.word1, test.word2))
		fmt.Println(&quot;Runes:        &quot;, commonRunes(test.word1, test.word2))
		fmt.Print(&quot;Bytes &amp; Runes: &quot;)
		fmt.Println(commonBytesRunes(test.word1, test.word2))
	}
}

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()

参考这个示例

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

英文:

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

See this example:

package main

import &quot;fmt&quot;
import &quot;unicode/utf8&quot;

func index(s1, s2 string) int {
	res := 0
	for i, w := 0, 0; i &lt; len(s2); i += w {
		if i &gt;= 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 := &quot;日本本a語&quot;
	bar := &quot;日本本b語&quot;
	fmt.Println(index(foo, bar))
	foo = &quot;日本語&quot;
	bar = &quot;日otest&quot;
	fmt.Println(index(foo, bar))
	foo = &quot;\xF0&quot;
	bar = &quot;\xFF&quot;
	fmt.Println(index(foo, bar))
}

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字符。

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

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

英文:

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

package main

import (
	&quot;fmt&quot;
)

func equal(s1, s2 string) int {
	eq := 0
	if len(s1) &gt; 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(&quot;buzzfizz&quot;, &quot;buzz&quot;))
	fmt.Println(equal(&quot;Makan&quot;, &quot;Makon&quot;))
	fmt.Println(equal(&quot;Indah&quot;, &quot;Ihkasyandehlo&quot;))
}

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:

确定