Replace all spaces in the line with +

I have a line, and I want to replace each space of this line with +, I'm tired of this, using:

tw.Text = strings.Replace(tw.Text, " ", "+", 1) 

But this did not work for me ... any solutions?

For example, a line might look like this:

 The answer of the universe is 42 
+7
source share
3 answers

from Go documentation: func Replace

If n <0, the number of replacements is not limited.

to try

 strings.Replace(tw.Text, " ", "+", -1) 
+18
source

Documentation at strings.Replace() : http://golang.org/pkg/strings/#Replace

According to the documentation, the fourth integer parameter is the number of replacements. Your example will replace the first place with "+". You need to use a number less than 0 so that it does not impose a restriction:

 tw.Text = strings.Replace(tw.Text, " ", "+", -1) 
+4
source

If you use this in a query, the QueryEscape method provided by net/url is the best solution: https://golang.org/pkg/net/url/#QueryEscape

 import "net/url" tw.Text = url.QueryEscape(tw.Text) 
0
source

All Articles