Golang 检查一个字符串是否为数字
在本教程中,我们将学习如何使用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:在一个函数中检查字符串是否为数字
步骤
- 步骤1 − 导入fmt和regexp包。
-
步骤2 − 开始主函数。
-
步骤3 − 声明字符串和布尔类型的变量。
-
步骤4 − 初始化字符串变量。
-
步骤5 − 调用函数
regexp.MustCompile()
和MatchString()
。 -
步骤6 − 初始化布尔值为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"
)
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
代码描述
-
在上面的程序中,我们首先声明主包
-
然后导入包含fmt包文件的fmt包
-
之后,我们导入实现正则表达式搜索的regexp包
-
现在开始函数 main() .GO程序的执行从函数main()开始
-
接下来我们声明字符串类型和布尔类型的变量。Word是字符串类型变量,numeric是布尔类型变量
-
随后,我们调用两个函数 regexp.MustCompile() (此函数创建正则表达式)和 MatchString() 函数(此函数返回一个布尔值,指示字符串是否匹配模式)
-
最后,使用 fmt.Println() 打印结果,判断一个字符串是否为数字。此函数在fmt包下定义,用于写入标准输出。
示例2:Golang程序代码,检查一个字符串是否为数字,使用两个独立的函数
步骤
-
步骤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
代码的描述
-
在上面的程序中,我们首先声明了主包。
-
我们需要导入包含fmt包文件的fmt包。
-
除此之外,我们还将导入实现正则表达式搜索的regexp包。
-
接下来,我们创建了一个函数is_numeric(),以判断给定的字符串是否是数字。
-
在函数is_numeric()中,我们调用了两个内置函数regexp.MustCompile()(该函数创建正则表达式)和MatchString()函数(该函数返回一个布尔值,指示是否匹配字符串的模式)。
-
现在开始main()函数。GO程序从main()函数开始执行。
-
然后,我们声明一个字符串变量’word’,并将其初始化为需要查看该字符串值是否为数字的值。
-
下一步是调用函数is_numeric(),并将结果存储在布尔变量isnumeric中。
-
最后,使用fmt.Println()打印结果,判断字符串是否为数字。
结论
在上述两个示例中,我们成功编译并执行了Golang程序代码,以检查字符串是否为数字。
Golang中的regexp包是一个内建包,可以用于编写任何复杂度的正则表达式。我们使用regexp包调用内置函数:(1)MustCompile()函数编译正则表达式并返回实例。(2)MatchString()函数将比较正则表达式与传递的字符串。如果正则表达式匹配字符串,则简单返回true;否则返回false,表示模式不匹配。因此,通过使用MustCompile和MatchString函数,我们可以验证任何字符串以检查其是否为数字。