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


在Go語言中,我們有多種資料型別來儲存資料,其中字串資料型別用於儲存一系列字元。字串變數的大小為1或8位。另一方面,陣列是一種資料型別,用於在連續的記憶體位置儲存資料。

語法

func fields(s string) []string

Fields() 函式位於 strings 包中,用於將字串轉換為字串陣列。此函式接收一個字串值作為引數,並返回相應的陣列作為結果。

func split(s, sep string) []string

Split() 函式接收兩個字串作為引數,一個是我們要分割的字串,另一個是我們希望用來分割字串的字元或字母。該函式返回最終輸出,該輸出是一個包含分割後獲得的字串的陣列。

func typeofobject(x interface{})

TypeOf() 函式用於獲取任何變數的型別。此函式位於 reflect 包中,它接收要確定其型別的變數作為引數。然後,該函式返回指定變數的型別作為結果。

方法 1:使用 Fields() 函式

在此方法中,我們將使用一個名為 Fields() 的內建函式。

演算法

  • 步驟 1 - 首先,我們需要定義 main 包。

  • 步驟 2 - 然後匯入 fmt 和 strings 包。

  • 步驟 3 - 呼叫 main() 函式。

  • 步驟 4 - 初始化並定義一個名為 s 的字串,併為其賦值。此外,在螢幕上列印此字串。

  • 步驟 5 - 現在呼叫 strings 包中定義的 fields() 函式,並將上述初始化的字串作為引數傳遞給它。該函式透過執行轉換返回結果。

  • 步驟 6 - 將函式返回的陣列儲存在單獨的變數中,並使用 fmt.Println() 函式在螢幕上列印結果。

示例

在此示例中,我們將使用 fields() 函式將字串轉換為陣列。

package main
import (
   "fmt"
   "reflect"
   "strings"
)

func main() {
   // initializing the string variable and assign value to it
   var s string = "this is a sentence lets break it !"
   fmt.Println("The given data is:\n", s, "and its type is", reflect.TypeOf(s))
   arrayOfString := strings.Fields(s)
   fmt.Println()
   fmt.Println("The array obtained from the string is:\n", arrayOfString)
}

輸出

The given data is:
 this is a sentence lets break it ! and its type is string

The array obtained from the string is:
 [this is a sentence lets break it !]

方法 2:使用 Split() 函式

在此方法中,我們將使用 split() 函式將陣列轉換為字串。

演算法

  • 步驟 1 - 首先,我們需要定義 main 包並匯入 fmt 和 strings 包。

  • 步驟 2 - 接下來,呼叫 main() 函式。

  • 步驟 3: - 初始化並定義一個名為 s 的字串,併為其賦值,即我們要分割的字串。初始化一個名為 sep 的空字串變數,併為其賦值。

  • 步驟 4 - 現在,使用 fmt.Println() 函式在螢幕上列印字串。

  • 步驟 5 - 這將是 s 字串將被分隔的值。將“ ”(空格)儲存在 sep 變數中,因為我們希望獲得字串的每個單詞,但以陣列格式。

  • 步驟 6 - 現在呼叫 strings 包中定義的 split() 函式,並將字串以及 sep 變數作為引數傳遞給它。該函式將根據提供的變數分割字串並返回相應的陣列。

  • 步驟 7 - 將函式返回的字串陣列儲存在單獨的變數中,並使用 fmt.Println() 函式在螢幕上列印結果。

示例

以下是使用 split() 函式將字串轉換為陣列的 Go 語言程式。

package main
import (
   "fmt"
   "reflect"
   "strings"
)

func main() {
   // initializing the string variable
   var s string = "this is a sentence lets break it !"
   var sep string = " "
   fmt.Println("The given data is:\n", s, "and its data type is:", reflect.TypeOf(s))
   arrayOfString := strings.Split(s, sep)
   fmt.Println()
   fmt.Println("The array obtained after splitting the string is:\n", arrayOfString)
}

輸出

The given data is:
 this is a sentence lets break it ! and its data type is: string

The array obtained after splitting the string is:
 [this is a sentence lets break it !]

方法 3:使用 FOR 迴圈

在此方法中,我們將使用 for 迴圈將字串轉換為陣列。

演算法

  • 步驟 1 - 首先,我們需要定義 main 包。

  • 步驟 2 - 然後匯入 fmt 和 reflect 包。

  • 步驟 3 - 呼叫 main() 函式。

  • 步驟 4 - 初始化並定義一個名為 s 的字串,併為其賦值。此外,在螢幕上列印此字串。

  • 步驟 5 - 現在使用 for 迴圈遍歷陣列,並將字串的每個元素追加到新陣列中。

  • 步驟 6 - 最後,使用 fmt.Println() 函式在螢幕上列印結果。

示例

以下是使用 for 迴圈將字串轉換為陣列的 Go 語言程式

package main

import (
   "fmt"
   "reflect"
)

func main() {
   str := "hello world"
   fmt.Println("The given data is:\n", str, "and its data type is:", reflect.TypeOf(str))
   arr := make([]string, len(str))
   for i, r := range str {
      arr[i] = string(r)
   }
   fmt.Println()
   fmt.Println("The array obtained by the above string is:",arr)
}

輸出

The given data is:
 hello world and its data type is: string

The array obtained by the above string is: [h e l l o   w o r l d]

結論

我們已成功編譯並執行了一個 Go 語言程式,該程式將字串轉換為陣列,並附帶示例。我們為此任務使用了 strings 庫中定義的 fields() 和 split() 預定義函式。這些函式接收字串作為引數,並在執行任務後返回相應的字串陣列。

更新於: 2023年2月16日

7K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.