Home »
Golang »
Golang Reference
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)
Parameters
- 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 »