Golang Program to Check if a String is Numeric


In this tutorial, we will learn how to check if a given string is numeric or not using Go programming language. We will be using the regexp package in Go to validate the numeric patterning of the string.

Examples of numeric string − A string which has a numeric value in it

T20CRICKET
GOOD1
100PERCENT

Syntax

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

Example 1: Golang Program Code to check if a String is Numeric or Not in a Single Function

Algorithm

  • Step 1 − Import the package fmt and package regexp.

  • Step 2 − Start function main.

  • Step 3 − Declare the variables of type string and Boolean.

  • Step 4 − Initialize the string variable.

  • Step 5 − Calling the functions regexp.MustCompile() and MatchString().

  • Step 5 − Initializing the Boolean value to numeric.

  • Step 5 − Print the result using fmt.Println().

Example

// 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) }

Output

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

Description of the Code

  • In the above program, we first declare the package main

  • Then Import the fmt package that includes the files of package fmt

  • After that we import regexp package that implements regular expression search

  • Now start the function main().GO program execution starts with the function main()

  • Next we declare the variables of string type and Boolean type. Word is string type variable and numeric is a Boolean variable

  • Later on we call the two functions regexp.MustCompile() (this function creates the regular expression) and MatchString() function (this function returns a Boolean value that indicates whether a pattern is matched by the string)

  • Finally printing the result if a string is numeric or not using fmt.Println().This function is defined under the fmt package and it helps to write standard output.

Example 2: Golang program code to check if a String is Numeric or Not in two Separate Functions

Algorithm

  • Step 1 − Import the package fmt and package regexp.

  • Step 2 − Create the function is_numeric().

  • Step 3 − Calling the functions regexp.MustCompile() and MatchString().

  • Step 4 − Start function main().

  • Step 5 − Declare the string variable and initialize it.

  • Step 6 − Calling the function is_numeric().

  • Step 7 − Print the final result using fmt.Println().

Example

// 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 }

Output

Golang program to check if a string is numeric

Given String = Good

it is not numeric

Description of the Code

  • In the above program, we first declare the package main.

  • We have to import the fmt package that includes the files of package fmt.

  • Along with that we will import regexp package that implements regular expression search.

  • Next we create a function is_numeric() to identify if the given string is numeric or not.

  • In the function is_numeric(), we are calling two in-built functions regexp.MustCompile() (this function creates the regular expression) and MatchString() function (this function returns a Boolean value that indicates whether a pattern is matched by the string).

  • Now start the function main().GO program execution starts with the function main().

  • After that we declare a string variable ‘word’ and initialize it with a value where you need to find if that string value is numeric or not.

  • Then step is to call the function is_numeric() and storing the result in the Boolean variable isnumeric.

  • Finally printing the result if a string is numeric or not using fmt.Println().

Conclusion

In the above two examples we have successfully compiled and executed the Golang program code to check if a string is numeric or not.

The regexp package in Golang is a inbuilt package that allows you to write regular expressions of any complexity. We use the regexp package to call the inbuilt functions: (1)MustCompile() function which

compiles a regular expression and returns the instance. And (2) MatchString() function which will compare the regular expression to the passed string. It will simply return true if the regular expression evaluated is matched with the string else false if the pattern is not matched. Hence by using the MustCompile and MatchString functions, we can validate any string to check if it is numeric or not.

Updated on: 15-Nov-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements