ADVERTISEMENT
ADVERTISEMENT

Golang strconv.Unquote() Function with Examples

Golang | strconv.Unquote() Function: Here, we are going to learn about the Unquote() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 10, 2021

strconv.Unquote()

The Unquote() function is an inbuilt function of the strconv package which is used to interpret the given string s as a single-quoted, double-quoted, or backquoted,  and returns the string value that s quotes. (If there is a single-quoted string, the function returns the corresponding one-character string.)

It accepts a parameter (s) and returns the string value that s quotes.

Syntax:

func Unquote(s string) (string, error)

Parameter(s):

  • s : The quoted string value from which we have to get the string.

Return Value:

The return type of the Unquote() function is a (string, error), it returns the string value that the given string (s) quotes.

Example 1:

// Golang program to demonstrate the
// example of strconv.Unquote() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.Unquote("Hello, world! How are you?"))
	fmt.Println(strconv.Unquote("\"Hello, world! How are you?\""))
	fmt.Println(strconv.Unquote("`Hello, world! How are you?`"))
	fmt.Println(strconv.Unquote("'\u2639'"))
}

Output:

 invalid syntax
Hello, world! How are you? <nil>
Hello, world! How are you? <nil>
☹ <nil>

Example 2:

// Golang program to demonstrate the
// example of strconv.Unquote() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	str, err := strconv.Unquote("Hello, world!")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("\"Hello, world!\"")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("`Hello, world!`")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("'\u263a'")
	fmt.Printf("%q, %v\n", str, err)

	str, err = strconv.Unquote("'\u2639\u2639'")
	fmt.Printf("%q, %v\n", str, err)
}

Output:

"", invalid syntax
"Hello, world!", <nil>
"Hello, world!", <nil>
"☺", <nil>
"", invalid syntax

Golang strconv Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.