Go 語言程式演示字串連線


什麼是字串連線?

在 Go 中,字串連線是將兩個或多個字串組合成單個字串的過程。Go 中有幾種方法可以連線字串。我們將在本文中討論它們。

方法 1:使用使用者定義函式

我們建立的函式將以兩個字串作為引數,並將透過將這兩個字串組合為結果來返回結果字串。

演算法

  • 步驟 1 − 首先,我們需要匯入 fmt 包。

  • 步驟 2 − 然後建立一個函式,將兩個字串連線在一起。該函式接受要連線的兩個字串,並透過使用“+”運算子將它們連線在一起,返回最終形成的字串。

  • 步驟 3 − 然後,啟動 main() 函式。在 main() 內部初始化兩個字串型別的變數併為其賦值。此外,在螢幕上列印字串。

  • 步驟 4 − 透過將兩個字串作為引數傳遞給 concatenation() 函式來呼叫它,並將獲得的結果儲存在一個名為 string 的變數中。

  • 步驟 5 − 現在,我們需要使用 fmt.Println() 函式在螢幕上列印最終結果。

示例

在此示例中,我們將使用 Go 程式語言中的外部函式來演示如何將兩個字串連線在一起以形成一個字串。

package main
import "fmt"

// function to join two strings together
func concatenate(s1, s2 string) string {
   return s1 + s2
}
func main() {
   var s1 string = "This is the first string, "
   var s2 string = "This is the second string"
   fmt.Println("The first string is:\n", s1)
   fmt.Println()
   fmt.Println("The second string is:\n", s2)
   result := concatenate(s1, s2)
   fmt.Println()
   fmt.Println("The final string obtained by concatenating the above strings together is:\n", result)
}

輸出

The first string is:
 This is the first string, 

The second string is:
 This is the second string

The final string obtained by concatenating the above strings together is:
 This is the first string, This is the second string

方法 2:使用內部包

在這種方法中,我們將編寫一個 Go 語言程式,使用內部函式演示字串連線。

語法

func Join(s []string, sep string) string

join 函式用於將陣列轉換為字串。此函式存在於 strings 包中。它接受兩個引數,第一個是我們希望轉換的陣列,第二個是將陣列元素轉換為字串後應使用的分隔符,並返回最終字串。

buffer.WriteString() 

WriteString() 函式存在於 bytes 包中。此函式允許我們在緩衝區記憶體中寫入字串字元。如果我們多次呼叫此函式,它不會修改字串。而是透過將其與下一個字串連線等來更新字串。

演算法

  • 步驟 1 − 首先,我們需要匯入 fmt 和 strings 包。

  • 步驟 2 − 現在,啟動 main() 函式。在此函式內部初始化兩個字串變數併為其賦值。

  • 步驟 3 − 在螢幕上列印這些字串。

  • 步驟 4 − 現在,使用內部函式進行字串連線

  • 步驟 5 − 然後,該函式在將兩個字串組合在一起作為結果後返回字串。

  • 步驟 6 − 將獲得的結果儲存在一個變數中,並使用 fmt.Println() 函式在螢幕上列印它。

示例 1

以下是使用 strings.Join() 函式演示字串連線的示例。

package main
import (
   "fmt"
   "strings"
)

func main() {
   var s1 string = "Life is what happens\t"
   var s2 string = "when you're busy making other plans."
   fmt.Println("The first string is:\n", s1)
   fmt.Println()
   fmt.Println("The second string is:\n", s2)
   var result string = strings.Join([]string{s1, s2}, "")
   fmt.Println()
   fmt.Println("The final string obtained by concatenating the above strings together is:\n", result)
}

輸出

The first string is:
 Life is what happens	

The second string is:
 when you're busy making other plans.

The final string obtained by concatenating the above strings together is:
 Life is what happens	when you're busy making other plans.

示例 2

在此示例中,我們將編寫一個 Go 語言程式,使用 bytes 包演示字串連線。

package main
import (
   "bytes"
   "fmt"
)

func main() {
   var buffer bytes.Buffer
   var s1 string = "Life is what happens\t"
   var s2 string = "when you're busy making other plans."
   fmt.Println("The first string is:\n", s1)
   fmt.Println()
   fmt.Println("The second string is:\n", s2)
   buffer.WriteString(s1)
   buffer.WriteString(s2)

   var result string = buffer.String()
   fmt.Println()
   fmt.Println("The final string obtained by concatenating the above strings together is:\n", result)
}

輸出

The first string is:
 Life is what happens	

The second string is:
 when you're busy making other plans.

The final string obtained by concatenating the above strings together is:
 Life is what happens	when you're busy making other plans.

結論

我們已經成功編譯並執行了一個 Go 語言程式來演示字串連線以及示例。在這裡,我們使用了 Go 語言中的外部函式和庫函式來獲得結果。

更新於: 2023-02-13

155 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告