英文:
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 (
"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))
}
}
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 "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))
}
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 (
"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"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论