简体   繁体   English

golang 将“type []string”转换为字符串

[英]golang convert "type []string" to string

I see some people create a for loop and run through the slice as to create a string, is there an easier way to convert a []string to a string ?我看到有些人创建了一个for循环并遍历切片以创建一个字符串,有没有更简单的方法将[]string转换为string

Will sprintf do it? sprintf会这样做吗?

You can use strings.Join(arr []string, separator string) string , as in pretty much any other language I know您可以使用strings.Join(arr []string, separator string) string ,就像我知道的几乎任何其他语言一样

https://golang.org/pkg/strings/#Join https://golang.org/pkg/strings/#Join

this is simple example, which You can paste to main function:这是一个简单的示例,您可以将其粘贴到主函数中:

  stringArray := []string {"Hello","world","!"}
  justString := strings.Join(stringArray," ")
  fmt.Println(justString)

And link to working example on playground.并链接到操场上的工作示例

Or using very simple function simple function或者使用非常简单的函数simple function

Will Sprint do it? Sprint会这样做吗?

Yes indeed!确实是的!

Here is another way to convert to a string if all you care about is that it is a string and not specifically how it looks (see answers above with strings.Join for a little more flexibility).如果您只关心它是一个字符串而不是它的具体外观,那么这是另一种转换为字符串的方法(请参阅上面带有strings.Join答案以获得更大的灵活性)。

The advantage of this method (or variations such as Sprintf ), is it will work with (almost) every other data such as maps and structs and any custom type that implements the fmt.Stringer inteface.这种方法(或Sprintf变体)的优点是它可以(几乎)与所有其他数据一起使用,例如mapsstructs以及实现fmt.Stringer接口的任何自定义类型。

  stringArray := []string {"Hello","world","!"}
  justString := fmt.Sprint(stringArray)

Here is a link to a working example .这是一个工作示例的链接。

It can be done easily using Join function by importing strings package.通过导入字符串包,可以使用 Join 函数轻松完成。 You need to pass the slice of strings and the separator you need to separate the elements in the string.您需要传递字符串切片和分隔字符串中元素所需的分隔符。 (examples: space or comma) (例如:空格或逗号)

func Join(elems []string, sep string) string

Example Code :示例代码:

package main

import (
    "fmt"
    "strings"
)

func main() {
    sliceStr := []string{"a","b","c","d"}
    str := strings.Join(sliceStr,", ")
    fmt.Println(str)
}

//output: a, b, c, d

If you dont care about the separator, you can use path :如果您不关心分隔符,则可以使用path

package main
import "path"

func main() {
   a := []string{"south", "north"}
   s := path.Join(a...)
   println(s == "south/north")
}

https://golang.org/pkg/path#Join https://golang.org/pkg/path#Join

package main

import (
"fmt"
"reflect"
"strings"
)

func main() {
str1 := []string{"Trump", "In", "India", "On", "Feb 25"}
fmt.Println(str1)
fmt.Println(reflect.TypeOf(str1))

str2 := strings.Join(str1, " ")
fmt.Println(str2)
fmt.Println(reflect.TypeOf(str2))

str3 := strings.Join(str1, ", ")
fmt.Println(str3)
fmt.Println(reflect.TypeOf(str3))
}

Below is the ouput of the above program:-以下是上述程序的输出:-

go run hello.go
[Trump In India On Feb 25]
[]string
Trump In India On Feb 25
string
Trump, In, India, On, Feb 25
string

In the above code, first, we have defined a slice of string and then use the reflect package to determine the datatype of the slice.在上面的代码中,我们首先定义了一个字符串切片,然后使用反射 package 来确定切片的数据类型。 We have imported the “strings” module.我们已经导入了“字符串”模块。 With strings.Join() method, and we combine all elements of a string slice into a string.使用 strings.Join() 方法,我们将字符串切片的所有元素组合成一个字符串。 So, Golang string.Join() function that converts slice to string.因此,Golang string.Join() function 将切片转换为字符串。 We have passed the space(” “) as a delimiter.我们已经通过空格(“”)作为分隔符。 So we will join the slice elements by space.所以我们将按空格连接切片元素。

The second argument to strings.Join() is the delimiter. strings.Join() 的第二个参数是分隔符。 For no delimiter, please use an empty string literal.如果没有分隔符,请使用空字符串文字。

In the next step, we have again used the TypeOf() function to check the data type.在下一步中,我们再次使用 TypeOf() function 来检查数据类型。

Then we have used the Golang string.Join() function again, but this time, we have passed (,) Comma.然后我们又使用了 Golang 的 string.Join() function ,不过这一次,我们通过了 (,) 逗号。 So, command separated values will be returned, which is also a type of string.因此,将返回命令分隔值,这也是一种字符串。

So, if you want to get CSV values in Golang, then you can use the Go string.Join() method.所以,如果你想在 Golang 中获取 CSV 值,那么你可以使用 Go string.Join() 方法。

You can also try with functions:-您也可以尝试使用以下功能:-

// abc.go
package main

type deck []string

func (cards deck) toString() string {
    // converts slice to string
    return strings.Join([]string(cards), ",")

}


//main.go
package main

import "fmt"

func main() {
    cards := []string {"Trump", "In", "India", "On", "Feb 25"}
    fmt.Println(cards.toString())

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM