Take the 2-minute tour ×

I have a string and I want to replace every space in this string with a + I tired this by using:

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

But that didn't worked for me...any solutions?

For example the string could look like:

The answer of the universe is 42
share|improve this question

2 Answers 2

up vote 8 down vote accepted

from the Go documentation: func Replace



strings.Replace(tw.Text, " ", "+", -1)
share|improve this answer

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

According to the documentation, the fourth integer parameter is the number of replacements. Your example would only replace the first space with a "+". You need to use a number less than 0 for it to not impose a limit:

tw.Text = strings.Replace(tw.Text, " ", "+", -1)
share|improve this answer

Your Answer


