Golang : Strip slashes from string example
A quick post on how to get an equivalent to PHP's stripslashes()
function in Golang. Basically in PHP, the stripslashes()
function will remove any backslashes from a string. \'
becomes '
and double backslashes \\
are made into a single backslash \
.
Example from official PHP documentation :
<?php
$str = "Is your name O\'reilly?";
echo stripslashes($str);
?>
Output :
Is your name O'reilly?
To achieve similar result in Golang, use this example :
package main
import (
"fmt"
"strings"
)
func main() {
// use backtick ` instead of double quote "
// otherwise, you will get this error message
// unknown escape sequence: '
str := `Is your name O\'reilly?`
fmt.Println(str)
stripSlash := strings.Replace(str, "\\", "", -1)
fmt.Println(stripSlash)
}
Output :
Is your name O\'reilly?
Is your name O'reilly?
NOTE :
Golang's strconv.Unquote()
function does not strip backslashes. What it does is to transform the a Go character literal to the corresponding one-character.
Happy coding!
Reference :
See also : Golang : unknown escape sequence error
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7k Swift : Convert (cast) String to Float
+10.5k Golang : Change date format to yyyy-mm-dd
+7.4k Golang : Generate Datamatrix barcode
+12.7k Golang : How to convert a number to words
+11.9k Golang : List objects in AWS S3 bucket
+14.2k Golang : How to add color to string?
+3.8k Python : Find out the variable type and determine the type with simple test
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+3.8k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+13.3k Golang : Parsing or breaking down URL
+8.1k Golang : Create and shuffle deck of cards example
+6.5k Golang : Command line ticker to show work in progress