📌  相关文章
📜  如何在 Golang 中使用 strconv.QuoteRune()函数?

📅  最后修改于: 2021-10-25 02:34:46             🧑  作者: Mango

Go 语言提供内置支持,以通过strconv Package实现基本数据类型的字符串表示的转换。该包提供了一个QuoteRune()函数,用于查找表示符文的单引号 Go字符串字面量,返回的字符串使用 Go 转义序列(\t、\n、\xFF、\u0100)来控制字符和不可打印通过isprint判断定义的字符。要访问 QuoteRune()函数,您需要在程序中导入strconv 包

句法:

func QuoteRune(rn rune) string

参数:该函数接受一个符文类型的参数,即rn。

返回值:此函数返回一个单引号 Go字符串字面量,它代表符文。

示例 1:

// Golang program to illustrate 
// strconv.QuoteRune() Function
package main

import (
    "fmt"
    "strconv"
)

func main() { 

    // Finding a single-quoted Go 
    // string literal representing rune
    // Using QuoteRune() function
    r := strconv.QuoteRune('♥')
    fmt.Println(r)

}

输出:

'♥'

示例 2:

// Golang program to illustrate
// strconv.QuoteRune() Function
package main

import (
    "fmt"
    "strconv"
)

func main() {

    // Finding a single-quoted Go 
    // string literal representing rune
    // Using QuoteRune() function
    val1 := strconv.QuoteRune('♣')
    fmt.Println("Result 1: ", val1)
    fmt.Println("Length 1: ", len(val1))

    val2 := strconv.QuoteRune('→')
    fmt.Println("Result 2: ", val2)
    fmt.Println("Length 2: ", len(val2))

}

输出:

Result 1:  '♣'
Length 1:  5
Result 2:  '→'
Length 2:  5