Golang strconv.UnquoteChar() Function with Examples

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

strconv.UnquoteChar()

The UnquoteChar() function is an inbuilt function of the strconv package which is used to decode the first character or byte in the escaped string or character literal represented by the string s. It returns four values (value of quote character, multibyte status, tail string, and an error),

  1. value, a rune type decoded Unicode code point value (i.e., the quote character).
  2. multibyte, a bool type value (true or false) that indicates whether the decoded character requires a multibyte UTF-8 representation or not.
  3. tail, a string, the remaining string after the first quote character, and
  4. error, an error that will be nil if the character is syntactically valid.

It accepts two parameters (s string, quote byte) and returns the four values explained above.

Syntax:

func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)

Parameter(s):

  • s : The quoted string.
  • quote : A byte value to be checked.

Return Value:

The return type of the UnquoteChar() function is a (value rune, multibyte bool, tail string, err error), it returns four values (value of quote character, multibyte status, tail string and an error)

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

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

Output:

34 false Hello, world! How are you?\" 
96 false Hello, world! How are you?` 
34 false \u2639Hello world!\" 
0 false  invalid syntax

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	value, mb, tail, err := strconv.UnquoteChar(`\"Hello, world!\"`, '"')
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println("value:", string(value))
	fmt.Println("multibyte:", mb)
	fmt.Println("tail:", tail)
	fmt.Println()

	value, mb, tail, err = strconv.UnquoteChar("\"Hello, world!\"", '"')
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println("value:", string(value))
	fmt.Println("multibyte:", mb)
	fmt.Println("tail:", tail)
	fmt.Println()
}

Output:

value: "
multibyte: false
tail: Hello, world!\"

Error: invalid syntax
value: 
multibyte: false
tail: 

Golang strconv Package »




Comments and Discussions!

Load comments ↻





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