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


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

Go 語言提供內置支持,以通過 strconv 包實現與基本數據類型的字符串表示之間的轉換。這個包提供了一個 CanBackquote() 函數,用於檢查字符串 str 是否可以不變地表示為單行反引號字符串,除了製表符之外沒有控製字符。要訪問 CanBackquote() 函數,您需要借助 import 關鍵字在程序中導入 strconv 包。

用法:

func CanBackquote(str string) bool

參數:該函數接受一個字符串類型的參數,即 str。

返回值:如果字符串 str 可以不變地表示為單行反引號字符串,則此函數返回 true,否則返回 false。

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



範例1:


// Golang program to illustrate
// strconv.CanBackquote() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
    // Checking whether the given
    // string can be represented
    // unchanged as a single-line 
    // backquoted string
    // Using CanBackquote() function
    fmt.Println(strconv.CanBackquote("Welcome to GeeksforGeeks"))
    fmt.Println(strconv.CanBackquote("`Welcome to GeeksforGeeks`"))
    fmt.Println(strconv.CanBackquote(`"Welcome to GeeksforGeeks"`))
  
}

輸出:

true
false
true

範例2:


// Golang program to illustrate
// strconv.CanBackquote() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
// Main function
func main() {
  
    // Checking whether the given
    // string can be represented
    // unchanged as a single-line
    // backquoted string
    // Using CanBackquote() function
    res:= strconv.CanBackquote("Welcome to GeeksforGeeks")
    if res == true {
        fmt.Println("The given string is unchanged "+
                     "single-line backquoted string.")
    } else {
        fmt.Println("The given string is a "+
         "changeable single-line backquoted string.")
    }
  
}

輸出:

The given string is unchanged single-line backquoted string.



相關用法


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