How to Remove Backslash from String in Go

In Go, you can “use the strings.Replace() or strings.ReplaceAll() functions”  to remove backslash from a string.

The difference between these two functions is that strings.Replace() function accepts an additional argument to specify the number of replacements to make, while strings.ReplaceAll() function replaces all occurrences.

Method 1: Using the strings.ReplaceAll() function

To remove all backslashes (\) from a string in Go, you can use the strings.ReplaceAll() function.

Example 1

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := `This is a string with some backslashes: \hello\ \world\`
  fmt.Println("Original String:", str)

  // Using strings.ReplaceAll to remove all backslashes
  strWithoutBackslashes := strings.ReplaceAll(str, `\`, "")
  fmt.Println("String without backslashes:", strWithoutBackslashes)
}

Output

Remove Backslash from String in Go

Example 2

In Go, raw string literals are represented using backticks (“). Within these literals, backslashes are treated as regular characters and not as escape characters.

For example, in the raw string literal “\hello\world“, the backslashes are just regular characters.

To remove all backslashes(\) from a raw string literal, the process remains the same as before.

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := `\This is a raw string literal with some backslashes: \hello\ \world\`
  fmt.Println("Original String:", str)

  // Using strings.ReplaceAll to remove all backslashes
  strWithoutBackslashes := strings.ReplaceAll(str, `\`, "")
  fmt.Println("String without backslashes:", strWithoutBackslashes)
}

Output

Original String: \This is a raw string literal with some backslashes: 
                 \hello\ \world\
String without backslashes: This is a raw string literal with some backslashes: 
                 hello world

Method 2: Using the strings.Replace() function

If you were to use strings.Replace(), you need to specify a large enough count to ensure all occurrences are replaced, or use -1 to replace all occurrences.

Example 3

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := `This is a string with some backslashes: \hello\ \world\`
  fmt.Println("Original String:", str)

  // Using strings.Replace to remove all backslashes
  strWithoutBackslashes := strings.Replace(str, `\`, "", -1)
  fmt.Println("String without backslashes:", strWithoutBackslashes)
}

Output

Original String: This is a string with some backslashes: 
                 \hello\ \world\
String without backslashes: This is a string with some backslashes:
                 hello world

Example 4

To remove all backslashes from the raw string literal using strings.Replace() function, you can do with -1 as the count to replace all occurrences.

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := `\This is a raw string literal with some backslashes: \hello\ \world\`
  fmt.Println("Original String:", str)

  // Using strings.Replace to remove all backslashes
  strWithoutBackslashes := strings.Replace(str, `\`, "", -1)
  fmt.Println("String without backslashes:", strWithoutBackslashes)
}

Output

Original String: \This is a raw string literal with some backslashes: 
                 \hello\ \world\
String without backslashes: This is a raw string literal with some backslashes: 
                 hello world

That’s it!

Related posts

Check if a string is alphanumeric

Check if a string contains substring

Encode a string in Go

Trim a string in Go

Index of a string in Go

Leave a Comment