Go 语言提供内置支持,以通过 strconv 包实现与基本数据类型的字符串表示之间的转换。该包提供了一个 QuoteToGraphic() 函数,用于查找表示 str 的 double-quoted Go 字符串文字,返回的字符串保留 IsGraphic 定义的 Unicode 图形字符,不变并使用 Go 转义序列(\t、\n、\xFF、\u0100)对于非图形字符。要访问 QuoteToGraphic() 函数,您需要借助 import 关键字在程序中导入 strconv 包。
用法:
func QuoteToGraphic(str string) string
参数:该函数接受一个字符串类型的参数,即 str。
返回值:此函数返回代表 str 的 double-quoted Go 字符串文字。
让我们在给定示例的帮助下讨论这个概念:
范例1:
// Golang program to illustrate
// strconv.QuoteToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a double-quoted Go
// string literal representing str
// Using func QuoteToGraphic() function
str:= strconv.QuoteToGraphic("\u2665 Welcome GeeksforGeeks \u2665")
fmt.Println (str)
}
输出:
"♥ Welcome GeeksforGeeks ♥"
范例2:
// Golang program to illustrate
// strconv.QuoteToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a double-quoted Go
// string literal representing rune
// Using QuoteToGraphic() function
val1:= strconv.QuoteToGraphic(`"I like Δ "`)
fmt.Println("Result 1:", val1)
fmt.Println("Length 1:", len(val1))
val2:= strconv.QuoteToGraphic("I love \u2666")
fmt.Println("Result 2:", val2)
fmt.Println("Length 2:", len(val2))
}
输出:
Result 1: "\"I like Δ\t\"" Length 1: 17 Result 2: "I love ♦" Length 2: 12
相关用法
- Golang strconv.QuoteRune()用法及代码示例
- Golang strconv.Quote()用法及代码示例
- Golang strconv.QuoteRuneToGraphic()用法及代码示例
- Golang strconv.FormatBool()用法及代码示例
- Golang strconv.CanBackquote()用法及代码示例
- Golang strconv.FormatUint()用法及代码示例
- Golang strconv.IsGraphic()用法及代码示例
- Golang strconv.IsPrint()用法及代码示例
- Golang strconv.Itoa()用法及代码示例
- Golang strconv.QuoteRuneToASCII()用法及代码示例
- Golang strconv.QuoteToASCII()用法及代码示例
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.AddInt64()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang io.PipeWriter.CloseWithError()用法及代码示例
注:本文由纯净天空筛选整理自ankita_saini大神的英文原创作品 How to use strconv.QuoteToGraphic() Function in Golang?。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
