Go語言程式檢查給定字串是否為空


在 Go 中,字串是一系列字元。它是一種不可變的資料型別,這意味著一旦建立了一個字串,就不能修改它。字串用雙引號 ("") 括起來,可以包含任意組合的字母、數字和符號。它們通常用於儲存文字,並且經常用作程式中的輸入和輸出。

語法

func len(v Type) int

len() 函式用於獲取任何引數的長度。它將要查詢長度的資料型別變數作為引數,並返回一個整數,即該變數的長度。

func TrimSpace(str string) string

trimSpace() 函式位於 strings 包中,用於刪除字串字元之間的空格。該函式接受所需的字串作為引數,並在刪除單詞之間的空格後返回最終字串。

演算法

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

  • 步驟 2 - 現在,啟動一個名為 isEmpty() 的函式。

  • 步驟 3 - 使用 for 迴圈遍歷字串。

  • 步驟 4 - 現在,啟動 main() 函式。在 main() 中初始化一個字串變數併為其賦值。

  • 步驟 5 - 透過將字串變數作為引數傳遞給函式來呼叫 isEmpty() 函式。

  • 步驟 6 - 使用 if 條件檢查函式返回的值是 true 還是 false。

  • 步驟 7 - 如果函式返回的值為 true,則列印字串為空,否則列印初始化的變數中存在的字串。

示例 1

在此示例中,我們將使用 for 迴圈透過在外部函式中使用 for 迴圈來檢查給定字串是否為空。我們建立的函式將接受要檢查的字串作為引數,並根據字串中是否包含字元返回一個布林變數。

package main
import "fmt"
func isEmpty(str string) bool {
   var empty bool = true
   for i := 0; i < len(str); i++ {
      if str[i] != ' ' {
         empty = false
         break
		}
	}
   return empty
}
func main() {
	var input string
	input = "Easier way to succeed is by trying"
	fmt.Println("Getting first string")
	if isEmpty(input) {
		fmt.Println("The string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Getting second string")
	input = ""
	if isEmpty(input) {
		fmt.Println("The string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

輸出

Getting first string
The given string is:
 Easier way to succeed is by trying

Getting second string
The string is empty.

示例 2

在此示例中,我們將使用 Go 語言中的內建函式 len() 來查詢給定字串是否為空。

package main
import "fmt"
func main() {
	var input string
	fmt.Println("Example 1:")
	input = "This is a string"
	var result int = len(input)
	if result == 0 {
		fmt.Println("The string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Example 2:")
	result = 0
	input = ""
	if result == 0 {
		fmt.Println("The given string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

輸出

Example 1:
The given string is:
 This is a string

Example 2:
The given string is empty.

示例 3

在此示例中,我們將使用 strings 包中的 TrimSpace() 函式來檢查給定字串是否為空。

package main
import (
	"fmt"
	"strings"
)
func main() {
	var input, result string
	result = ""
	input = "Try and try until you get the success"
	fmt.Println("Example 1:")
	if result == strings.TrimSpace(input) {
		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Example 2:")
	input = ""
	if result == strings.TrimSpace(input) {
		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

輸出

Example 1:
The given string is:
 Try and try until you get the success

Example 2:
The given string is empty

示例 4

在此示例中,我們將使用 reflect 包中的 ValueOf() 函式來檢查給定字串是否為空。

package main
import (
	"fmt"
	"reflect"
)
func main() {
	var input string
	input = "This is some string."
	fmt.Println("Example 1:")
	if reflect.ValueOf(input).String() == "" {
		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Example 2:")
	input = ""
	if reflect.ValueOf(input).String() == "" {

		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

輸出

Example 1:
The given string is:
 This is some string.

Example 2:
The given string is empty

結論

我們已經成功編譯並執行了一個 Go 語言程式來檢查給定字串是否為空,並附帶示例。在這裡,我們使用了庫函式和 for 迴圈來實現此結果。

更新於: 2023年2月13日

1K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告