Go語言程式:將字元轉換為字串


在本文中,我們將學習如何在 Go 程式語言中將字元轉換為字串。

字元 - Go 語言沒有 char 資料型別,相反,Go 語言中的字元由 rune 資料型別表示。Rune 表示一個以 UTF-8 格式編碼的字元值。rune 的大小為 32 位。

字串 - 字串資料型別用於儲存一系列字元。它可以是文字或字母的形式。字串變數的大小為 1 位元組或 8 位。

有兩種方法可以實現此結果。我們將在本文中討論它們。

語法

func QuoteRune(r rune) string
func string(n int) string

QuoteRune() 函式在 strconv 包中定義,用於將 rune 或字元值轉換為字串。此函式以 rune 值作為輸入,並將其轉換為返回字串形式的輸出。

演算法

  • 步驟 1 - 匯入所需的包,命名為 fmt、strconv 和 reflect。

  • 步驟 2 - 啟動 main() 函式。

  • 步驟 3 - 初始化一個 rune 型別的變數併為其賦值。

  • 步驟 4 - 呼叫 QuoteRune() 方法並將 rune 變數作為引數傳遞給它。

  • 步驟 5 - 將獲得的結果儲存在另一個字串型別的變數中。

  • 步驟 6 - 使用 fmt.Println() 函式將結果列印到螢幕上,以及變數的型別。

示例 1

使用 QuoteRune() 方法將字元轉換為字串的 Go 語言程式。

package main

import (
   "fmt"
   "reflect"
   "strconv"
)
// fmt package allows us to print anything on the screen.
// strconv package allows us to use other pre-defined functions like QuoteRune().
// reflect package allows us to use methods that allow knowing the type of a variable.

// calling the main function
func main() {

   // initializing and defining a rune variable and assigning a value to it.
   var r rune = '☺'

   // using the QuoteRune() function defined in strconv package to convert runr to
      // string.
   // storing the string obtained in a new variable
   var s string = strconv.QuoteRune(r)

   // printing the type and value of the rune character
   fmt.Println("The given variable is of type rune and has value of", r)

   // printing the type and value of the string obtained from the rune.
   fmt.Println("The obtained variable is of type", reflect.TypeOf(s), "and has value of", s)
}

輸出

The given variable is of type rune and has value of 9786
The obtained variable is of type string and has value of '☺'

描述

  • 首先,我們需要匯入 fmt、strconv 和 reflect 包。fmt 包允許我們在螢幕上列印任何內容,strconv 包允許我們使用其他預定義函式,如 QuoteRune(),而 reflect 包允許我們使用允許瞭解變數型別的方法。

  • 呼叫 main() 函式,這是我們程式的起點。

  • 現在初始化一個 rune 型別的變數併為其賦值。

  • 接下來,我們需要呼叫 strconv 包中定義的方法,該方法將允許我們使用 QuoteRune() 方法將 rune 變數作為引數傳遞給此函式,並且此函式將在將其轉換為字串後返回 rune 的相應字串等價物。

  • 現在將獲得的結果儲存在另一個字串型別的變數中。

  • 使用 fmt.Println() 函式在螢幕上列印 rune 值,並在下一行列印獲得的字串等價物及其型別。

示例 2

使用 String() 函式將字元轉換為字串的 Go 語言程式。

package main

import (
   "fmt"
   "reflect"
)
// fmt package allows us to print anything on the screen.
// reflect package allows us to use methods that allow knowing the type of a variable.

func runesToString(runes []rune) (outString string) {
   // don't need index so _
   for _, v := range runes {
      outString += string(v)
   }
   return
}

// calling the main funciton
func main() {

   // initializing and defining a rune variable and assigning a value to it.
   var r []rune = []rune{'a', 'b', 'z'}

   // Calling the runesToString() function to convert array of runes to string.
   // storing the string obtained in a new variable
   var s string = runesToString(r)

   // printing the type and value of the rune character
   fmt.Println("The given variable is of type rune and has value of", r)

   // printing the type and value of the string obtained from the rune.
   fmt.Println("The obtained variable is of type", reflect.TypeOf(s), "and has value of", s)
}

輸出

The given variable is of type rune and has value of [97 98 122]
The obtained variable is of type string and has value of abz

描述

  • 首先,我們需要匯入 fmt、strconv 和 reflect 包。fmt 包允許我們在螢幕上列印任何內容,reflect 包允許我們使用允許瞭解變數型別的方法。

  • 初始化一個 RuneToString() 函式以將 rune 陣列轉換為字串。此函式將以包含 rune 的陣列作為引數,並在成功轉換後返回其相應的字串等價物。

  • 呼叫 main() 函式,這是我們程式的起點。

  • 現在初始化一個 rune 型別的陣列併為其賦值。

  • 接下來,我們需要呼叫 RuneToString() 方法。

  • 現在將獲得的結果儲存在另一個變數中。

  • 使用 fmt.Println() 函式在螢幕上列印 rune 值,並在下一行列印獲得的字串等價物及其型別。

更新於: 2022-12-22

4K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.