在大多数语言中都有内置的支持,例如:
python:
> A = [1,2,3]
> ,.join([a(a)for a])
'1,2,3'
go:
包主
导入(
字节
fmt
strconv
)
//找不到这样一行代码:(。
func arrayToString (A [] int,delim字符串)string {
var buffer bytes.Buffer
for i:= 0; i< len(A); i ++ {
buffer。 WriteString(strconv.Itoa(A [i]))
if i!= len(A)-1 {
buffer.WriteString(delim)
}
}
return buffer.String()
}
func main(){
A:= [] int {1,2,3}
fmt.Println(arrayToString(A,,))
}
当然必须是一个埋入去的实用程序,允许我用一行代码执行此操作?
我知道有 strings.Join(A, ,),但这只有在A已经是[]字符串的情况下才有效。
转换
A:= [] int {1,2,3,4,5,6,7,8,9}
添加到单行分隔的字符串,例如
1,2,3,4,5,6,7,8,9
use:
strings.Trim(strings.Join(strings.Fields(fmt.Sprint(A )),delim),[])
或:
strings.Trim(strings.Join(strings.Split(fmt.Sprint(A),),delim),[])
或:
strings.Trim(strings.Replace(fmt.Sprint(A),,delim,-1),[])
并从一个函数中返回它,例如:
package main
导入fmt
导入字符串
func arrayToString(a [] int,delim字符串)string {
返回strings.Trim(字符串。替换(fmt.Sprint(a),,delim,-1),[])
//返回strings.Trim(strings.Join(strings.Split(fmt.Sprint(a), ),delim),[])
//返回strings.Trim(strings.Join( strings.Fields(fmt.Sprint(a)),delim),[])
}
func main(){
A:= [] int {1 ,2,3,4,5,6,7,8,9}
fmt.Println(arrayToString(A,,))// 1,2,3,4,5, 6,7,8,9
}
要在逗号后加上一个空格,您可以调用 arrayToString(A,,)或者相反将返回定义为返回strings.Trim(strings.Replace(fmt.Sprint(a), ,delim +,-1),[])强制插入分隔符之后。
Basically I have []int{1, 2, 3}, I want a one-liner that transforms this into the string "1, 2, 3" (I need the delimiter to be custom, sometimes ., sometimes ,, etc). Below is the best I could come up with. Searched online and did not seem to find a better answer.
In most languages there is in-built support for this, e.g.:
python:
> A = [1, 2, 3] > ", ".join([str(a) for a in A]) '1, 2, 3'
go:
package main import ( "bytes" "fmt" "strconv" ) // Could not find a one-liner that does this :(. func arrayToString(A []int, delim string) string { var buffer bytes.Buffer for i := 0; i < len(A); i++ { buffer.WriteString(strconv.Itoa(A[i])) if i != len(A)-1 { buffer.WriteString(delim) } } return buffer.String() } func main() { A := []int{1, 2, 3} fmt.Println(arrayToString(A, ", ")) }
Surely there must be an utility buried into go that allows me to do this with a one-liner?
I know that there is strings.Join(A, ", "), but that only works if A is already []string.
解决方案To convert
A := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
to a one line delimited string like
"1,2,3,4,5,6,7,8,9"
use:
strings.Trim(strings.Join(strings.Fields(fmt.Sprint(A)), delim), "[]")
or:
strings.Trim(strings.Join(strings.Split(fmt.Sprint(A), " "), delim), "[]")
or:
strings.Trim(strings.Replace(fmt.Sprint(A), " ", delim, -1), "[]")
and return it from a function such as in this example:
package main import "fmt" import "strings" func arrayToString(a []int, delim string) string { return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]") //return strings.Trim(strings.Join(strings.Split(fmt.Sprint(a), " "), delim), "[]") //return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(a)), delim), "[]") } func main() { A := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} fmt.Println(arrayToString(A, ",")) //1,2,3,4,5,6,7,8,9 }
To include a space after the comma you could call arrayToString(A, ", ") or conversely define the return as return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim + " ", -1), "[]") to force its insertion after the delimiter.
这篇关于单线程将[] int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!