Golang strconv.QuoteToASCII() Function with Examples

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

strconv.QuoteToASCII()

The QuoteToASCII() 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 non-ASCII 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 s.

Syntax:

func QuoteToASCII(s string) string

Parameter(s):

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

Return Value:

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

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

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

Output:

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

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x string
	var result string

	x = `"☺Aspire to inspire before we expire."`
	result = strconv.QuoteToASCII(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.QuoteToASCII(x)
	fmt.Printf("%T, %d,%v\n", x, len(x), x)
	fmt.Printf("%T, %d,%v\n", result, len(result), result)
}

Output:

string, 40,"☺Aspire to inspire before we expire."
string, 47,"\"\u263aAspire 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 »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.