當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang strconv.QuoteRuneToGraphic()用法及代碼示例

Go 語言提供內置支持,以通過 strconv 包實現與基本數據類型的字符串表示之間的轉換。這個包提供了一個 QuoteRuneToGraphic() 函數,用於查找代表符文的 single-quoted Go 字符文字。如果符文不是 IsGraphic 定義的 Unicode 圖形字符,則返回的字符串將使用 Go 轉義序列(\t、\n、\xFF、\u0100)。要訪問 QuoteRuneToGraphic() 函數,您需要借助 import 關鍵字在程序中導入 strconv 包。

用法:

func QuoteRuneToGraphic(rn rune) string

參數:此函數采用符文類型的一個參數,即 rn。

返回值:此函數返回代表符文的 single-quoted Go 字符串文字。

讓我們在給定示例的幫助下討論這個概念:



範例1:

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

import (
    "fmt"
    "strconv"
)

func main() {

    // Finding a single-quoted Go
    // string literal representing rune
    // Using func QuoteRuneToGraphic() function
    str:= strconv.QuoteRuneToGraphic('♥')
    fmt.Println (str)
    
}

輸出:

'♥'

範例2:

// Golang program to illustrate 
// strconv.QuoteRuneToGraphic() Function
package main
 
import (
    "fmt"
    "strconv"
)
 
func main() {

    // Finding a single-quoted Go 
    // string literal representing rune
    // Using QuoteRuneToGraphic() function
    val1:= strconv.QuoteRuneToGraphic('®')
    fmt.Println("Result 1:", val1)
   
    val2:= strconv.QuoteRuneToGraphic('\u2666')
    fmt.Println("Result 2:", val2)

     val3:= strconv.QuoteRuneToGraphic('\u000a')
    fmt.Println("Result 3:", val3) 
}

輸出:

Result 1: '®'
Result 2: '♦'
Result 3: '\n'

相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 How to use strconv.QuoteRuneToGraphic() Function in Golang?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。