猿问

从 Go 中的字符串整数中获取整数片段?

我想将表示整数的字符串转换为包含构成初始字符串的每个整数的切片。

游乐场链接

我试过使用Atoi()中的函数strconv,它给出了“不能使用 toCheck[i](字节类型)作为 strconv.Atoi 参数中的类型字符串”。我也尝试过 int() 转换,但它给了我我认为是 ASCII 值的值——我知道我可以减去 48,但这似乎有点老套。

package main


import (

    "fmt"

)


func main() {


toCheck := "987654321"


    var toSum []int

    for i := len(toCheck) - 2; i >= 0; i = i-2 {

        toSum = append(toSum, int(toCheck[i]))

    }


    fmt.Println(toSum)


    // expected 2 4 6 8

    // actual 50 52 54 56

}

预期输出:2 4 6 实际:50 52 54 56


慕容3067478
浏览 129回答 3
3回答

神不在的星期二

但它给了我我认为是 ASCII 值的值——我知道我可以减去 48,但这似乎有点老套。这不是黑客。您有一个表示 ascii 数字的字节,您希望将其值作为 0-9 之间的整数获取。你就是这样做的。这也是将字符串转换为整数的一般工作方式。您可以使用此方法将每个单独的字节转换为数字。“0”的 UTF-8 表示不会很快改变。当然,48您可以减去而不是减去'0'。为了使其更通用,也许您还应该返回一个布尔值,指示该字节是否可以转换。func ByteToInt(c byte) (int, bool) {&nbsp; &nbsp; if c >= '0' && c <= '9' {&nbsp; &nbsp; &nbsp; &nbsp; return int(c - '0'), true&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return 0, false&nbsp; &nbsp; }}您可以在您的代码示例中使用它,它会返回您的预期结果:package mainimport (&nbsp; &nbsp; "fmt")func ByteToInt(c byte) (int, bool) {&nbsp; &nbsp; if c >= '0' && c <='9' {&nbsp; &nbsp; &nbsp; &nbsp; return int(c - '0'), true&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return 0, false&nbsp; &nbsp; }}func main() {toCheck := "987654321"&nbsp; &nbsp; var toSum []int&nbsp; &nbsp; for i := len(toCheck) - 2; i >= 0; i = i-2 {&nbsp; &nbsp; &nbsp; &nbsp; var digit, _ = ByteToInt(toCheck[i])&nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, digit)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(toSum)}https://play.golang.org/p/MRqtgY0ugZY输出是你所期望的:[2 4 6 8]

慕后森

例如,package mainimport (&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; toCheck := "987654321"&nbsp; &nbsp; fmt.Println(toCheck)&nbsp; &nbsp; toSum := make([]int, 0, len(toCheck)/2)&nbsp; &nbsp; for i := len(toCheck) - 2; i >= 0; i -= 2 {&nbsp; &nbsp; &nbsp; &nbsp; c := toCheck[i]&nbsp; &nbsp; &nbsp; &nbsp; if c >= '0' && c <= '9' {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, int(c-'0'))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(len(toSum), cap(toSum), toSum)}游乐场: https:&nbsp;//play.golang.org/p/wtIgqEKj-Bk输出:987654321 4&nbsp;4&nbsp;[2&nbsp;4&nbsp;6&nbsp;8]这是围棋。代码应该相当高效。例如,$ go test sum_test.go -bench=. -benchmemBenchmarkPeterSO-8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;50000000&nbsp; &nbsp; &nbsp;24.5 ns/op&nbsp; &nbsp; 32 B/op&nbsp; &nbsp; 1 allocs/opBenchmarkTom-8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;20000000&nbsp; &nbsp; &nbsp;77.6 ns/op&nbsp; &nbsp; 56 B/op&nbsp; &nbsp; 3 allocs/opBenchmarkUser10753492-8&nbsp; &nbsp; 20000000&nbsp; &nbsp; &nbsp;79.0 ns/op&nbsp; &nbsp; 56 B/op&nbsp; &nbsp; 3 allocs/opBenchmarkGrissom-8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;20000000&nbsp; &nbsp; 108 ns/op&nbsp; &nbsp; &nbsp; 56 B/op&nbsp; &nbsp; 3 allocs/op$sum_test.go:package mainimport (&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "testing")// https://play.golang.org/p/wtIgqEKj-Bkfunc BenchmarkPeterSO(b *testing.B) {&nbsp; &nbsp; toCheck := "987654321"&nbsp; &nbsp; for N := 0; N < b.N; N++ {&nbsp; &nbsp; &nbsp; &nbsp; toSum := make([]int, 0, len(toCheck)/2)&nbsp; &nbsp; &nbsp; &nbsp; for i := len(toCheck) - 2; i >= 0; i -= 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c := toCheck[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if c >= '0' && c <= '9' {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, int(c-'0'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// https://play.golang.org/p/KgQrbesy5rTfunc BenchmarkTom(b *testing.B) {&nbsp; &nbsp; toCheck := "987654321"&nbsp; &nbsp; for N := 0; N < b.N; N++ {&nbsp; &nbsp; &nbsp; &nbsp; var toSum []int&nbsp; &nbsp; &nbsp; &nbsp; for i := len(toCheck) - 2; i >= 0; i = i - 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, int(toCheck[i]))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func ByteToInt(c byte) (int, bool) {&nbsp; &nbsp; if c >= '0' && c <= '9' {&nbsp; &nbsp; &nbsp; &nbsp; return int(c - '0'), true&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return 0, false&nbsp; &nbsp; }}// https://play.golang.org/p/MRqtgY0ugZYfunc BenchmarkUser10753492(b *testing.B) {&nbsp; &nbsp; toCheck := "987654321"&nbsp; &nbsp; for N := 0; N < b.N; N++ {&nbsp; &nbsp; &nbsp; &nbsp; var toSum []int&nbsp; &nbsp; &nbsp; &nbsp; for i := len(toCheck) - 2; i >= 0; i = i - 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var digit, _ = ByteToInt(toCheck[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, digit)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// https://play.golang.org/p/kNbQVn8GJ9Rfunc BenchmarkGrissom(b *testing.B) {&nbsp; &nbsp; toCheck := "987654321"&nbsp; &nbsp; for N := 0; N < b.N; N++ {&nbsp; &nbsp; &nbsp; &nbsp; var toSum []int&nbsp; &nbsp; &nbsp; &nbsp; for i := len(toCheck) - 2; i >= 0; i = i - 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v, _ := strconv.Atoi(string(toCheck[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, v)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

www说

使用strconv包进行转换&nbsp; for i := len(toCheck) - 2; i >= 0; i = i-2 {&nbsp; &nbsp; &nbsp; &nbsp; v,_ := strconv.Atoi(string(toCheck[i]))&nbsp; &nbsp; &nbsp; &nbsp; toSum = append(toSum, v)&nbsp; &nbsp; }strconv.Atoi 是 strconv.ParseInt(s,10,0) 的快捷方式。在你的操场上完成 https://play.golang.org/p/kNbQVn8GJ9R
随时随地看视频慕课网APP

相关分类

Go
我要回答