如何检查追加的切片是否等于一个字符串?

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

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 &quot;fmt&quot;

func main() {
	var s []string
	s = append(s, &quot;a&quot;)
	fmt.Println(s)
	for i := 0; i &lt;= 1000; i++ {
		s = append(s, i)
		if s == &quot;a10&quot; {
			fmt.Println(&quot;Worked&quot;, 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 &quot;strings&quot;

//...

if strings.Join(s, &quot;&quot;) == &quot;a10&quot; {
      fmt.Println(&quot;Worked&quot;, 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 (
	&quot;fmt&quot;
	&quot;strconv&quot;
	&quot;strings&quot;
)

func main() {
	var s []string
	s = append(s, &quot;a&quot;)
	fmt.Println(s)
	for i := 0; i &lt;= 1000; i++ {
		s = append(s, strconv.Itoa(i))
		if strings.Join(s, &quot;&quot;) == &quot;a01&quot; {
			fmt.Println(&quot;Worked&quot;, 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 (
	&quot;fmt&quot;
	&quot;strconv&quot;
)

func main() {
	var s := &quot;a&quot;
	fmt.Println(s)
	for i := 0; i &lt;= 1000; i++ {
		s += strconv.Itoa(i)
		if s == &quot;a01&quot; {
			fmt.Println(&quot;Worked&quot;, s)
			return
		} else {
			fmt.Println(s)
		}
	}
}

To reset the array every time, just make the main method:

for i := 0; i &lt;= 1000; i++ {
    s := []string{&quot;a&quot;}
	s = append(s, strconv.Itoa(i))
	if strings.Join(s, &quot;&quot;) == &quot;a10&quot; {
		fmt.Println(&quot;Worked&quot;, s)
		return
	} else {
		fmt.Println(s)
	}
}

huangapple
  • 本文由 发表于 2017年6月16日 01:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/44573301.html
匿名

发表评论

匿名网友

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

确定