Golang remove backslash from string [SOLVED]

In this tutorial we will share some examples to remove backslash from strings in Golang.

 

Use strings.Replace and strings.ReplaceAll function

Example-1: Remove one or more backslash from string

The idea for removing the backslash is to replace these characters with blank characters. Golang strings package provides Replace() and ReplaceAll() function to help us to do that.

Advertisement

func Replace(s, old, new string, n int) string: Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.

func ReplaceAll(s, old, new string) string: ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.

Example of removing backslash from strings:

package main

import (
	"fmt"
	"strings"
)

func main() {
	// use string literals to create a string contains backslash
	str1 := `Welcome \ to GoLinuxCloud \ Nice to meet you!`

	// double backslash
	str2 := "Today is Sunday \\ Have a nice weekend\\!"

	fmt.Println("With single backslash:", str1)
	fmt.Println("With double backslash:", str2)

	// using Replace() function to remove all backslash
	removeStr1Backslash := strings.Replace(str1, "\\", "", -1)

	// using Replace() function
	removeStr2Backslash := strings.ReplaceAll(str2, "\\", "")

	fmt.Println("After removing single backslash:", removeStr1Backslash)
	fmt.Println("After removing double backslash:", removeStr2Backslash)
}

Output:

With single backslash: Welcome \ to GoLinuxCloud \ Nice to meet you!
With double backslash: Today is Sunday \ Have a nice weekend\!
After removing single backslash: Welcome  to GoLinuxCloud  Nice to meet you!
After removing double backslash: Today is Sunday  Have a nice weekend!

We can see that there are 2 spaces between words when we replace backslash by empty character. If we want to make it better, we can replace two space with only one space using Replace() function as the below example:

	// using Replace() function 
	removeStr1Backslash := strings.Replace(str1, "\\", "", -1)
	removeStr1Backslash = strings.Replace(removeStr1Backslash, "  ", " ", -1)

	// using Replace() function
	removeStr2Backslash := strings.ReplaceAll(str2, "\\", "")
	removeStr2Backslash = strings.ReplaceAll(removeStr2Backslash, "  ", " ")

Output:

Advertisement
After removing single backslash: Welcome to GoLinuxCloud Nice to meet you!
After removing double backslash: Today is Sunday Have a nice weekend!

 

Example-2: Remove backslash declared with raw literal strings

In our previous article we have explained different ways to write or escape a backslash so that it can be used inside a string. Once of the methods is using raw literal string. So we will update our code above to declare backslash using raw literal strings and then try to remove those backslash using strings.Replace() and strings.ReplaceAll() function:

	// using Replace() function to remove all backslash
	removeStr1Backslash := strings.Replace(str1, `\`, "", -1)

	// using Replace() function
	removeStr2Backslash := strings.ReplaceAll(str2, `\`, "")

Here now we don't have to worry about escaping backslash and we can just directly declare and remove it from our strings:

With single backslash: Welcome \ to GoLinuxCloud \ Nice to meet you!
With double backslash: Today is Sunday \ Have a nice weekend\!
After removing single backslash: Welcome to GoLinuxCloud Nice to meet you!
After removing double backslash: Today is Sunday Have a nice weekend!

 

Summary

In this article, we learned about strings.Replace() and strings,ReplaceAll function to remove backslash in string and covered different examples.

 

References

https://go.dev/ref/spec#String_literals
https://pkg.go.dev/strings#Replace

 

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X