英文:
How to efficiently concatenate string array and string in golang
问题
我有一堆字符串和[]字符串在golang中,我需要将它们连接起来。但是由于某种原因,我得到了很多空格,我需要摆脱它们。
以下是代码:
tests := strings.TrimSpace(s[0])
dep_string := make ([]string, len(tests) + len(sfinal))
dep_string = append (dep_string,tests)
for _,v := range sfinal {
dep_string = append(dep_string,v)
}
fmt.Println("dep_String is ",dep_string)
输入:
s[0] = "filename"
sfinal = [test test1]
期望输出:
[filename test test1]
实际输出:
[ filename test test1]
这真的很奇怪;即使使用了TrimSpace,我仍然无法摆脱多余的空格。有没有更高效的方法来连接它们?
英文:
I have a bunch of strings and []strings in golang which I need to concatenate. But for some reason I am getting a lot of whitespaces along the way which I need to get rid of.
Here's the code
tests := strings.TrimSpace(s[0])
dep_string := make ([]string, len(tests) + len(sfinal))
dep_string = append (dep_string,tests)
for _,v := range sfinal {
dep_string = append(dep_string,v)
}
fmt.Println("dep_String is ",dep_string)
Input:
s[0] = "filename"
sfinal = [test test1]
expected output
[filename test test1]
actual output
[ filename test test1]
It's really weird; even after using TrimSpace I am not able to get rid of excess space. Is there any efficient way to concatenate them?
答案1
得分: 7
空格是由于dep_string中的所有空元素造成的。当你使用make函数时,它会创建一个指定长度和容量的切片,并填充了一堆空元素。然后,当你使用append时,它会看到切片已经达到了最大容量,扩展切片,然后在所有空元素之后添加你的元素。解决方法是创建一个容量足够容纳所有元素的切片,但初始长度为零:
dep_string := make([]string, 0, len(tests)+len(sfinal))
strings.TrimSpace是不必要的。你可以在http://blog.golang.org/slices上阅读更多信息。
英文:
The whitespace is due to all of the empty elements in dep_string. When you use the make function, it creates a slice with the specified length and capacity, filled with a bunch of nothing. Then, when you use append, it sees that the slice has reached its maximum capacity, extends the slice, then adds your elements, after all of the nothing. The solution is to make a slice with the capacity to hold all of your elements, but with initial length zero:
dep_string := make ([]string, 0, len(tests) + len(sfinal))
strings.TrimSpace is unnecessary. You can read more at http://blog.golang.org/slices
答案2
得分: 4
Bill DeRose和Saposhiente关于切片的工作原理是正确的。
至于解决问题的更简单的方法,你也可以这样做:
fmt.Println("join is", strings.Join(append(s[:1], sfinal...), " "))
英文:
Bill DeRose and Saposhiente are correct about how slices work.
As for a simpler way of solving your problem, you could also do (play):
fmt.Println("join is",strings.Join(append(s[:1],sfinal...)," "))
答案3
得分: 0
当你执行赋值语句dep_string := make([]string, len(tests) + len(sfinal))
时,Go语言会将分配的内存清零,因此dep_string
在前面有len(tests) + len(sfinal)
个空字符串。目前的写法是在所有这些清零的字符串之后追加到切片的末尾。
运行这个链接可以看到这些空白字符串在你的代码中的位置。你可以通过创建长度为0、容量为len(tests) + len(sfinal)
的切片来修复它。然后,你可以使用strings.Join
来连接它们。
英文:
When you do the assignment dep_string := make ([]string, len(tests) + len(sfinal))
, Go zeros out the allocated memory so dep_string
then has len(tests) + len(sfinal)
empty strings at the front of it. As it's written now you append to the end of the slice, after all those zeroed out strings.
Run this to see where those blanks are showing up in your code. You can fix it by making a slice of length 0 and capacity len(tests) + len(sfinal)
instead. You can then concatenate them by using strings.Join
.
答案4
得分: 0
这是一个简单而高效的解决方案。
package main
import "fmt"
func main() {
s := make([]string, 1, 4)
s[0] = "filename"
sfinal := []string{"test", "test1"}
dep_string := append(s[:1], sfinal...)
fmt.Println("dep_String is ", dep_string)
}
输出结果:
dep_String is [filename test test1]
英文:
Here's a simple and efficient solution to your problem.
package main
import "fmt"
func main() {
s := make([]string, 1, 4)
s[0] = "filename"
sfinal := []string{"test", "test1"}
dep_string := append(s[:1], sfinal...)
fmt.Println("dep_String is ", dep_string)
}
Output:
dep_String is [filename test test1]
答案5
得分: 0
当你执行赋值语句dep_string := make([]string, len(tests) + len(sfinal))
时,它会分配len(tests) + len(sfinal)
个空字符串,对于你的情况来说是10个空字符串。所以当你执行赋值语句fmt.Println("dep_String is ", dep_string)
时,它会打印出10个空字符串,因为fmt.Println(slice of string)
会在两个元素之间添加空格,所以它会打印出9个空格,然后在你添加元素后打印出[ filename test test1]
,这些空格是这10个空字符串之间的空格。
英文:
When you do the assignment dep_string := make ([]string, len(tests) + len(sfinal))
, it allocate len(tests) + len(sfinal)
null strings ,it is 10 null strings in your case,so when you do the assignment fmt.Println("dep_String is ",dep_string)
,it will print 10 null strings, because fmt.Println(slice of string)
will add blank between two elements,so it will print 9 blanks ,so it will prints [ filename test test1]
after you append, the whitespaces is the blanks between the 10 null string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论