Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all spaces in a string with +

Tags:

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 
like image 679
Micheal Perr Avatar asked Dec 31 '11 16:12

Micheal Perr


People also ask

How do you replace all spaces in a string %20?

Approach: Count the total spaces in a string in one iteration, say the count is spaceCount. Calculate the new length of a string by newLength = length + 2*spaceCount; (we need two more places for each space since %20 has 3 characters, one character will occupy the blank space and for rest two we need extra space)

How do you replace all spaces in a string in TypeScript?

Use the replace() method to remove all whitespace from a string in TypeScript, e.g. str. replace(/\s/g, '') .

How do you replace all spaces in a string in Python?

The easiest approach to remove all spaces from a string is to use the Python string replace() method. The replace() method replaces the occurrences of the substring passed as first argument (in this case the space ” “) with the second argument (in this case an empty character “”).


1 Answers

Use strings.ReplaceAll

tw.Text = strings.ReplaceAll(tw.Text, " ", "+") 

If you're using an older version of go (< 1.12), use strings.Replace with -1 as limit (infinite)

tw.Text = strings.Replace(tw.Text, " ", "+", -1) 
like image 168
MikeM Avatar answered Oct 24 '22 01:10

MikeM