Go 語言提供內置支持,以通過 strconv 包實現與基本數據類型的字符串表示之間的轉換。該包提供了一個 Quote() 函數,用於查找表示 str 的 double-quoted Go 字符串文字,返回的字符串使用 Go 轉義序列(\t、\n、\xFF、\u0100)來控製 IsPrint 定義的字符和不可打印字符.要訪問 Quote() 函數,您需要在程序中導入 strconv 包。
用法:
func Quote(str string) string
參數:該函數接受一個字符串類型的參數,即 str。
返回值:此函數返回代表 str 的 double-quoted Go 字符串文字。
範例1:
// Golang program to illustrate strconv.Quote() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a double-quoted Go
// string literal representing str
// Using Quote() function
str:= strconv.Quote(`" Hello
Welcome to GeeksforGeeks "`)
fmt.Println(str)
}
輸出:
"\" Hello \n Welcome to GeeksforGeeks \""
範例2:
// Golang program to illustrate
// strconv.Quote() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Finding a double-quoted Go string literal
// Using Quote() function
val1:= strconv.Quote(`"Hello! GFG "`)
fmt.Println("Result 1:", val1)
fmt.Println("Length 1:", len(val1))
val2:= strconv.Quote(`"Welcome!
GeeksforGeeks"`)
fmt.Println("Result 2:", val2)
fmt.Println("Length 2:", len(val2))
}
輸出:
Result 1: "\"Hello! GFG \"" Length 1: 19 Result 2: "\"Welcome!\n GeeksforGeeks\"" Length 2: 37
相關用法
- Golang strconv.QuoteRune()用法及代碼示例
- Golang strconv.QuoteRuneToGraphic()用法及代碼示例
- Golang strconv.FormatBool()用法及代碼示例
- Golang strconv.CanBackquote()用法及代碼示例
- Golang strconv.FormatUint()用法及代碼示例
- Golang strconv.IsGraphic()用法及代碼示例
- Golang strconv.IsPrint()用法及代碼示例
- Golang strconv.Itoa()用法及代碼示例
- Golang strconv.QuoteToGraphic()用法及代碼示例
- 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.Quote() Function in Golang?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。