ADVERTISEMENT
ADVERTISEMENT

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 »


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.