Golang strconv.QuoteToGraphic() Function with Examples

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

strconv.QuoteToGraphic()

The QuoteToGraphic() 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 leaves Unicode graphic characters, as defined by IsGraphic() function, unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100) for non-graphic characters.

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

Syntax:

func QuoteToGraphic(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 QuoteToGraphic() 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.QuoteToGraphic() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.QuoteToGraphic("\u263aHello, world!\u263a."))
	fmt.Println(strconv.QuoteToGraphic("Nothing is impossible \u263a\u263a\u263a"))
	fmt.Println(strconv.QuoteToGraphic("No pressure \u000a no diamonds."))
}

Output:

"☺Hello, world!☺."
"Nothing is impossible ☺☺☺"
"No pressure \n no diamonds."

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x string
	var result string

	x = "\u263aHello, world!\u263a."
	result = strconv.QuoteToGraphic(x)
	fmt.Printf("%T, %d,%v\n", x, len(x), x)
	fmt.Printf("%T, %d,%v\n", result, len(result), result)

	x = "Nothing is impossible \u263a\u263a\u263a"
	result = strconv.QuoteToGraphic(x)
	fmt.Printf("%T, %d,%v\n", x, len(x), x)
	fmt.Printf("%T, %d,%v\n", result, len(result), result)
}

Output:

string, 20,☺Hello, world!☺.
string, 22,"☺Hello, world!☺."
string, 31,Nothing is impossible ☺☺☺
string, 33,"Nothing is impossible ☺☺☺"

Golang strconv Package »





Comments and Discussions!

Load comments ↻






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