英文:
How to check if appended slice is equal to a string?
问题
计划是在切片中有一个字符串,比如"a",一旦它变成"a100",就停止运行一个for循环,该循环在其末尾添加数字。当我尝试下面的代码时,它显示"(mismatched types []string and string)",这是因为我必须将切片转换为字符串吗?如果是这样,我该如何做?谢谢。
package main
import "fmt"
func main() {
var s []string
s = append(s, "a")
fmt.Println(s)
for i := 0; i <= 1000; i++ {
s = append(s, fmt.Sprintf("a%d", i))
if s[len(s)-1] == "a10" {
fmt.Println("Worked", s)
break
} else {
fmt.Println(s)
}
}
}
代码已经进行了一些修改。我们使用fmt.Sprintf
函数将数字添加到字符串中,并使用s[len(s)-1]
来检查切片中的最后一个元素是否等于"a10"。如果是,则打印"Worked"并停止循环。
英文:
So the plan is to have a string such as "a" in the slice, and once that turns into "a100" for example then stop running a for loop which is adding numbers to the end of it. When I try the code below it says "(mismatched types []string and string)", is this because I have to convert the slice into a string? If so, how do I do that? Thanks.
package main
import "fmt"
func main() {
var s []string
s = append(s, "a")
fmt.Println(s)
for i := 0; i <= 1000; i++ {
s = append(s, i)
if s == "a10" {
fmt.Println("Worked", s)
} else {
fmt.Println(s)
}
}
}
答案1
得分: 1
你可以通过以下方式将[]string
转换为string
:
import "strings"
//...
if strings.Join(s, "") == "a10" {
fmt.Println("Worked", s)
} else {
fmt.Println(s)
}
你还需要将i
(一个int类型)转换为string
,最常用的方法是使用strconv.Itoa
。最终,你的代码可能如下所示:https://play.golang.org/p/gD5b5XFimJ
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
var s []string
s = append(s, "a")
fmt.Println(s)
for i := 0; i <= 1000; i++ {
s = append(s, strconv.Itoa(i))
if strings.Join(s, "") == "a01" {
fmt.Println("Worked", s)
return
} else {
fmt.Println(s)
}
}
}
不过,你也可以直接将字符串(或[]byte
)追加到字符串中,而不使用数组...
package main
import (
"fmt"
"strconv"
)
func main() {
var s := "a"
fmt.Println(s)
for i := 0; i <= 1000; i++ {
s += strconv.Itoa(i)
if s == "a01" {
fmt.Println("Worked", s)
return
} else {
fmt.Println(s)
}
}
}
要在每次循环中重置数组,只需将主方法修改为:
for i := 0; i <= 1000; i++ {
s := []string{"a"}
s = append(s, strconv.Itoa(i))
if strings.Join(s, "") == "a10" {
fmt.Println("Worked", s)
return
} else {
fmt.Println(s)
}
}
英文:
You can convert an []string
to a string` by doing:
import "strings"
//...
if strings.Join(s, "") == "a10" {
fmt.Println("Worked", s)
} else {
fmt.Println(s)
}
You also need to convert i
(an int) to a string
, most likely you would use strconv.Itoa
. In the end, you code would look something like this: https://play.golang.org/p/gD5b5XFimJ
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
var s []string
s = append(s, "a")
fmt.Println(s)
for i := 0; i <= 1000; i++ {
s = append(s, strconv.Itoa(i))
if strings.Join(s, "") == "a01" {
fmt.Println("Worked", s)
return
} else {
fmt.Println(s)
}
}
}
Although, you could also just not use an array and append to the string (or an []byte) directly...
package main
import (
"fmt"
"strconv"
)
func main() {
var s := "a"
fmt.Println(s)
for i := 0; i <= 1000; i++ {
s += strconv.Itoa(i)
if s == "a01" {
fmt.Println("Worked", s)
return
} else {
fmt.Println(s)
}
}
}
To reset the array every time, just make the main method:
for i := 0; i <= 1000; i++ {
s := []string{"a"}
s = append(s, strconv.Itoa(i))
if strings.Join(s, "") == "a10" {
fmt.Println("Worked", s)
return
} else {
fmt.Println(s)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论