Go語言程式獲取字串的子字串
Go 語言中的子字串是指較大字串的一部分。它透過提供起始索引和長度來指定,包含從起始索引開始到指定長度的原始字串中的字元。子字串中的字元仍然是原始字串的一部分,並共享相同的記憶體。
在 Go 中,字串是一系列字元。它是一種不可變的資料型別,這意味著一旦建立字串,就不能修改它。字串用雙引號 ("") 括起來,可以包含任意組合的字母、數字和符號。
方法 1:使用 TrimSpace 函式
在本例中,我們將編寫一個 Go 語言程式,使用外部函式從字串中獲取子字串。我們建立的函式將字串作為引數,以及要獲取字串元素的起始和結束變數。
語法
For Output Code pre class
trimSpace() 函式位於 strings 包中,用於刪除字串字元之間的空格。該函式接受所需的字串作為引數,並在刪除其單詞之間的空格後返回最終字串。
演算法
步驟 1 − 首先,我們需要匯入 fmt 和 strings 包。
步驟 2 − 建立一個函式,該函式將字串及其要獲取子字串的兩個索引作為引數,並返回子字串。
步驟 3 − 啟動 main() 函式。
步驟 4 − 在 main 函式內部初始化一個字串變數並在其中儲存值。
步驟 5 − 在螢幕上列印變數。
步驟 6 − 現在透過傳遞所需的引數來呼叫子字串函式,並將函式返回的結果儲存在一個變數中。
步驟 7 − 該變數包含給定索引之間的子字串。在螢幕上列印它們。
示例
使用外部函式從字串中獲取子字串的 Go 語言程式
package main
import (
"fmt"
"strings"
)
// Substring function to get a substring from the string
func Substring(str string, start, end int) string {
return strings.TrimSpace(str[start:end])
}
func main() {
// initializing a string
var str string = "Live young live free"
fmt.Println("The given string is:", str)
// Get substring from index 5 to 15
substring := Substring(str, 5, 15)
fmt.Println("The string values between the index 5 and 15 are:", substring)
}
輸出
The given string is: Live young live free The string values between the index 5 and 15 are: young live
方法 2:使用索引的概念
在本文中,我們將使用索引的概念來編寫一個 Go 語言程式,以從原始字串中獲取子字串。
演算法
步驟 1 − 首先,我們需要匯入 fmt 包。
步驟 2 − 啟動 main() 函式。
步驟 3 − 在 main() 內部初始化一個字串變數併為其賦值
步驟 4 − 使用 fmt.Println() 函式在螢幕上列印此陣列。
步驟 5 − 此外,初始化兩個整數型別變數以儲存陣列的起始和結束索引。
步驟 6 − 將獲得的結果儲存在一個名為 result 的變數中,並使用 fmt.Println() 函式在螢幕上列印此變數。
示例
使用索引概念從字串中獲取子字串的 Go 語言程式
package main
import "fmt"
func main() {
// initializing a string
var str string = "Prevention is better than cure"
fmt.Println("The given array is:", str)
var startInd int = 11
var endInd int = 20
result := str[startInd:endInd]
fmt.Println()
fmt.Println("The substring is required between", startInd, "and", endInd)
fmt.Println("Array obtained after getting elements between the above indices are:", result)
}
輸出
The given array is: Prevention is better than cure The substring is required between 11 and 20 Array obtained after getting elements between the above indices are: is better
方法 3:使用 Split 函式
在本例中,我們將使用 split 函式來獲取 Go 語言中字串的子字串。
語法
func Split(str, sep string) []string
Split() 函式用於透過提供的分隔符分割字串。此函式位於 strings 包中,它接受要分割的字串以及分隔符作為引數。然後,該函式返回最終的字串陣列作為結果。
演算法
步驟 1 − 首先,我們需要匯入 fmt 和 strings 包。
步驟 2 − 然後啟動 main() 函式。
步驟 3 − 在此函式內部初始化一個字串變數併為其賦值。
步驟 4 − 進一步在螢幕上列印字串。
步驟 5 − 使用 strings 包中的 split() 函式
步驟 6 − 將子字串中的元素索引儲存在一個新變數中。
步驟 7 − 現在,使用 fmt.Println() 函式在螢幕上列印子字串。
示例
使用 split 函式從字串中獲取子字串的 Go 語言程式
package main
import (
"fmt"
"strings"
)
func main() {
// Initializing a string
var str string = "Never give up"
fmt.Println("The given string is:", str)
// Split string into a slice of substrings
substrings := strings.Split(str, " ")
substring := substrings[1]
fmt.Println("The second element in the above string is:", substring)
}
輸出
The given string is: Never give up The second element in the above string is: give
結論
我們已經成功編譯並執行了一個 Go 語言程式,用於從給定的字串中獲取子字串以及示例。我們在這裡初始化了三個示例,在第一個示例中,我們使用了一個單獨的函式以及 trimSpace() 函式,而在第二個示例中,我們使用了索引的概念來實現結果。在第三個示例中,我們使用了 strings 包中的 split() 函式。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP