ADVERTISEMENT
ADVERTISEMENT

Golang strconv.Quote() Function with Examples

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

strconv.Quote()

The Quote() function is an inbuilt function of the strconv package which is used to get a double-quoted Go string literal representing the given string s. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters and non-printable characters as defined by IsPrint() function.

It accepts a parameter (s) and returns a double-quoted Go string literal representing the given string.

Syntax:

func Quote(s string) string

Parameter(s):

  • s : The string value which is to be returned with the double-quoted string with escape sequences.

Return Value:

The return type of Quote() function is a string, it returns a double-quoted Go string literal representing the given string.

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.Quote(`"Aspire to inspire before we expire."`))
	fmt.Println(strconv.Quote(`"Start where you are ☺☺☺"`))
	fmt.Println(strconv.Quote(`"No pressure, no diamonds."`))
}

Output:

"\"Aspire to inspire before we expire.\""
"\"Start where you are ☺☺☺\""
"\"No pressure, no diamonds.\""

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x string
	var result string

	x = `"Aspire to inspire before we expire."`
	result = strconv.Quote(x)
	fmt.Printf("%T, %d,%v\n", x, len(x), x)
	fmt.Printf("%T, %d,%v\n", result, len(result), result)

	x = `"Aspire to inspire 
	before we expire."`
	result = strconv.Quote(x)
	fmt.Printf("%T, %d,%v\n", x, len(x), x)
	fmt.Printf("%T, %d,%v\n", result, len(result), result)
}

Output:

string, 37,"Aspire to inspire before we expire."
string, 41,"\"Aspire to inspire before we expire.\""
string, 39,"Aspire to inspire 
	before we expire."
string, 45,"\"Aspire to inspire \n\tbefore we expire.\""

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.