ADVERTISEMENT
ADVERTISEMENT

Golang strconv.QuotedPrefix() Function with Examples

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

strconv.QuotedPrefix()

The QuotedPrefix() function is an inbuilt function of the strconv package which is used to get the quoted-string (as understood by Unquote) at the prefix of the given string. If the string does not start with a valid quoted string, QuotedPrefix() returns an error.

It accepts a parameter (s) and returns the quoted-string (as understood by Unquote) at the prefix of s.

Syntax:

func QuotedPrefix(s string) (string, error)

Parameter(s):

  • s : The string from which we have to get the quoted string.

Return Value:

The return type of the QuotedPrefix() function is a (string, error), it returns a quoted string at the prefix of the given string.

Example 1:

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

package main

import (
	"fmt"
	"strconv"
)

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

Output:

 invalid syntax
"Hello, world!" <nil>
`Hello, world!` <nil>
'☺' <nil>

Example 2:

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

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s, err := strconv.QuotedPrefix("not a quoted string")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.QuotedPrefix("\"double-quoted string\" with trailing text")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.QuotedPrefix("`or backquoted` with more trailing text")
	fmt.Printf("%q, %v\n", s, err)
	s, err = strconv.QuotedPrefix("'\u263a' is also okay")
	fmt.Printf("%q, %v\n", s, err)
}

Output:

"", invalid syntax
"\"double-quoted string\"", <nil>
"`or backquoted`", <nil>
"'☺'", <nil>

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.