Go 語言程式檢查字串是否為數字


在本教程中,我們將學習如何使用 Go 程式語言檢查給定的字串是否為數字。我們將使用 Go 中的 regexp 包來驗證字串的數字模式。

數字字串示例 - 包含數字值的字串

T20CRICKET
GOOD1
100PERCENT

語法

func MustCompile(str string) *Regexp
//This function creates the regular expression

func MatchString(pattern string, s string) (matched bool, err error)
// This function returns a Boolean value that indicates whether a pattern is matched by the string

示例 1:Go 語言程式程式碼,在一個函式中檢查字串是否為數字

演算法

  • 步驟 1 - 匯入 fmt 包和 regexp 包。

  • 步驟 2 - 啟動 main 函式。

  • 步驟 3 - 宣告字串型別和布林型別的變數。

  • 步驟 4 - 初始化字串變數。

  • 步驟 5 - 呼叫 regexp.MustCompile()MatchString() 函式。

  • 步驟 5 - 將布林值初始化為 numeric。

  • 步驟 5 - 使用 fmt.Println() 列印結果。

示例

// GOLANG PROGRAM TO CHECK IF A STRING IS NUMERIC // fmt package allows us to print anything on the screen. package main import ( "fmt" // The regexp package provides support for regular expressions "regexp" ) func main() { // declare the variables var word string var numeric bool // initialize the variables word = "T20cricket" fmt.Println("Give string = ",word) // calling regexp.MustCompile() function to create the regular expression // calling MatchString() function that returns a bool that indicates whether a pattern is matched by the string numeric = regexp.MustCompile(`\d`).MatchString(word) // print the result fmt.Println(" check if a string is numeric - True/False") fmt.Println(numeric) }

輸出

Give string =  T20cricket
 check if a string is numeric - True/False
true

程式碼描述

  • 在上面的程式中,我們首先宣告 package main

  • 然後匯入 fmt 包,其中包含 fmt 包的檔案

  • 之後我們匯入 regexp 包,它實現了正則表示式搜尋

  • 現在開始 main() 函式。GO 程式執行從 main() 函式開始

  • 接下來,我們宣告字串型別和布林型別的變數。Word 是字串型別變數,numeric 是布林變數

  • 稍後,我們呼叫兩個函式 regexp.MustCompile()(此函式建立正則表示式)和 MatchString() 函式(此函式返回一個布林值,指示字串是否與模式匹配)

  • 最後,使用 fmt.Println() 列印字串是否為數字的結果。此函式定義在 fmt 包下,它有助於寫入標準輸出。

示例 2:Go 語言程式程式碼,在兩個獨立的函式中檢查字串是否為數字

演算法

  • 步驟 1 - 匯入 fmt 包和 regexp 包。

  • 步驟 2 - 建立 is_numeric() 函式。

  • 步驟 3 - 呼叫 regexp.MustCompile() 和 MatchString() 函式。

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

  • 步驟 5 - 宣告字串變數並初始化它。

  • 步驟 6 - 呼叫 is_numeric() 函式。

  • 步驟 7 - 使用 fmt.Println() 列印最終結果。

示例

// GOLANG PROGRAM TO CHECK IF A STRING IS NUMERIC // fmt package allows us to print anything on the screen. package main import ( "fmt" // The regexp package provides support for regular expressions "regexp" ) // create a function to check if string is numeric func is_numeric(word string) bool { return regexp.MustCompile(`\d`).MatchString(word) // calling regexp.MustCompile() function to create the regular expression. // calling MatchString() function that returns a bool that // indicates whether a pattern is matched by the string. } // start the function main() func main() { fmt.Println("Golang program to check if a string is numeric") // declare the string variable var word string // initialize the string variable word = "Good" // calling the function is_numeric and storing // the result in the boolean variable isnumeric isnumeric := is_numeric(word) if isnumeric { fmt.Println("Given String =", word,"\nit is numeric") } else{ fmt.Println("Given String =", word,"\nit is not numeric") } // print the final answer }

輸出

Golang program to check if a string is numeric

Given String = Good

it is not numeric

程式碼描述

  • 在上面的程式中,我們首先宣告 package main。

  • 我們必須匯入 fmt 包,其中包含 fmt 包的檔案。

  • 除此之外,我們還將匯入 regexp 包,它實現了正則表示式搜尋。

  • 接下來,我們建立一個函式 is_numeric() 來識別給定的字串是否為數字。

  • is_numeric() 函式中,我們呼叫了兩個內建函式 regexp.MustCompile()(此函式建立正則表示式)和 MatchString() 函式(此函式返回一個布林值,指示字串是否與模式匹配)。

  • 現在開始 main() 函式。GO 程式執行從 main() 函式開始。

  • 之後,我們宣告一個字串變數“word”並用一個值初始化它,你需要找到該字串值是否為數字。

  • 然後是呼叫 is_numeric() 函式並將結果儲存在布林變數 isnumeric 中。

  • 最後,使用 fmt.Println() 列印字串是否為數字的結果。

結論

在以上兩個示例中,我們已成功編譯並執行了 Go 語言程式程式碼,以檢查字串是否為數字。

Go 語言中的 regexp 包是一個內建包,允許你編寫任意複雜度的正則表示式。我們使用 regexp 包來呼叫內建函式:(1)MustCompile() 函式,它

編譯正則表示式並返回例項。(2)MatchString() 函式,它將比較正則表示式與傳遞的字串。如果評估的正則表示式與字串匹配,則它將簡單地返回 true,否則如果模式不匹配,則返回 false。因此,透過使用 MustCompile 和 MatchString 函式,我們可以驗證任何字串以檢查它是否為數字。

更新於:2022年11月15日

9K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

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