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:Golang程序代码,在一个函数中检查一个字符串是否为数字
算法
- 第1步 – 导入软件包fmt和软件包regexp。
-
第2步 – 启动函数main。
-
第3步 – 声明字符串和布尔类型的变量。
-
第4步–初始化字符串变量。
-
第5步 – 调用函数 regexp.MustCompile() 和 MatchString()。
-
第6步– 将布尔值初始化为数值。
-
第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
代码的描述
-
在上面的程序中,我们首先声明包main
-
然后导入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
代码的描述
-
在上面的程序中,我们首先声明包main。
-
我们必须导入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()函数,它将比较正则表达式和传递的字符串。如果评估的正则表达式与字符串匹配,它将简单地返回真,如果模式不匹配,则返回假。因此,通过使用MustCompile和MatchString函数,我们可以验证任何字符串,检查它是否是数字。