golang convert "type []string" to string

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

golang convert "type []string" to string

问题

我看到有些人创建一个for循环,并通过遍历切片来创建一个字符串,有没有更简单的方法将[]string转换为string

sprintf能做到吗?

英文:

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?

Will sprintf do it?

答案1

得分: 114

你可以使用strings.Join(arr []string, separator string) string

英文:

You can use strings.Join(arr \[\]string, separator string) string.

答案2

得分: 61

这是一个简单的示例,你可以将其粘贴到主函数中:

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

并且这是在 playground 上的可运行示例

或者可以使用一个非常简单的函数:简单函数

英文:

This is a simple example, which you can paste into the 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

答案3

得分: 6

这是另一种将其转换为字符串的方法,如果你只关心它是一个字符串而不关心具体的外观(参见上面使用strings.Join的答案,可以获得更多的灵活性)。

这种方法(或者类似的Sprintf变体)的优点是它可以与(几乎)所有其他数据一起使用,例如mapsstructs,以及任何实现了fmt.Stringer接口的自定义类型。

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

这里是一个可运行的示例链接

英文:

> Will Sprint do it?
>
> 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).

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.

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

Here is a link to a working example.

答案4

得分: 4

可以使用Join函数轻松实现,只需导入strings包。您需要传递字符串切片和要在字符串中分隔元素的分隔符。(例如:空格或逗号)

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

示例代码:

package main

import (
	"fmt"
	"strings"
)

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

//输出:a, b, c, d
英文:

It can be done easily using Join function by importing strings package. 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

答案5

得分: 2

如果您不关心分隔符,可以使用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

英文:

If you don't care about the separator, you can use 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

答案6

得分: 0

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))
}

上面的代码输出如下:

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

在上面的代码中,首先我们定义了一个字符串切片,并使用 reflect 包来确定切片的数据类型。
我们导入了 "strings" 模块。使用 strings.Join() 方法,我们将字符串切片的所有元素组合成一个字符串。因此,Golang 的 string.Join() 函数将切片转换为字符串。我们传递了空格(" ")作为分隔符。因此,我们将通过空格将切片元素连接起来。

strings.Join() 的第二个参数是分隔符。如果不需要分隔符,请使用空字符串。

在下一步中,我们再次使用 TypeOf() 函数来检查数据类型。

然后,我们再次使用 Golang 的 string.Join() 函数,但这次我们传递了逗号(,)。因此,将返回逗号分隔的值,这也是一种字符串类型。

因此,如果你想在 Golang 中获取 CSV 值,可以使用 Go 的 string.Join() 方法。

你也可以尝试使用函数:

// abc.go
package main

type deck []string

func (cards deck) toString() 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())
}
英文:
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.
We have imported the “strings” module. With strings.Join() method, and we combine all elements of a string slice into a string. So, Golang string.Join() function that converts slice to string. 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. 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.

Then we have used the Golang string.Join() function again, but this time, we have passed (,) Comma. 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.

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())

}

huangapple
  • 本文由 发表于 2017年1月20日 13:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/41756412.html
匿名

发表评论

匿名网友

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

确定