Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a single character inside a string in Golang?

I am getting a physical location address from a user and trying to arrange it to create a URL that would use later to get a JSON response from Google Geocode API.

The final URL string result should be similar to this one, without spaces:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

I do not know how to replace white spaces in my URL string and have commas instead. I did read a little about the strings and regexp packages and I have created the following code:

package main  import (     "fmt"     "bufio"     "os"     "http" )  func main() {     // Get the physical address     r := bufio.NewReader(os.Stdin)       fmt.Println("Enter a physical location address: ")     line, _, _ := r.ReadLine()      // Print the inputted address     address := string(line)     fmt.Println(address) // Need to see what I'm getting      // Create the URL and get Google's Geocode API JSON response for that address     URL := "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=true"     fmt.Println(URL)      result, _ := http.Get(URL)     fmt.Println(result) // To see what I'm getting at this point } 

like image 759
moalf Avatar asked Nov 18 '11 23:11

moalf


People also ask

How do you replace a character in a string with a string?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

How do you replace a word in a string in Golang?

Replace() Function in Golang is used to return a copy of the given string with the first n non-overlapping instances of old replaced by new one. Here, s is the original or given string, old is the string that you want to replace. new is the string which replaces the old, and n is the number of times the old replaced.

How do you replace inside a string?

You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.


2 Answers

You can use strings.Replace.

package main  import (     "fmt"     "strings" )  func main() {     str := "a space-separated string"     str = strings.Replace(str, " ", ",", -1)     fmt.Println(str) } 

If you need to replace more than one thing, or you'll need to do the same replacement over and over, it might be better to use a strings.Replacer:

package main  import (     "fmt"     "strings" )  // replacer replaces spaces with commas and tabs with commas. // It's a package-level variable so we can easily reuse it, but // this program doesn't take advantage of that fact. var replacer = strings.NewReplacer(" ", ",", "\t", ",")  func main() {     str := "a space- and\ttab-separated string"     str = replacer.Replace(str)     fmt.Println(str) } 

And of course if you're replacing for the purpose of encoding, such as URL encoding, then it might be better to use a function specifically for that purpose, such as url.QueryEscape

like image 139
Evan Shaw Avatar answered Oct 19 '22 04:10

Evan Shaw


If you need to replace all occurrences of the character in the string, then use strings.ReplaceAll:

package main  import (     "fmt"     "strings" )  func main() {     str := "a space-separated string"     str = strings.ReplaceAll(str, " ", ",")     fmt.Println(str) } 
like image 42
pcampana Avatar answered Oct 19 '22 03:10

pcampana