Golang Program to Check if the String is Alphanumeric


In Go programming language, it is essential to check if a string contains alphanumeric characters or not. Alphanumeric characters are a combination of alphabets and numbers, and they are commonly used in passwords, usernames, and other important data. In this article, we will discuss how to write a Golang program to check if a string is alphanumeric.

What is an Alphanumeric String?

An alphanumeric string is a string that contains a combination of alphabets and numbers. It can also include special characters such as underscore (_) and dash (-), but not all special characters are considered alphanumeric. Alphanumeric strings are commonly used in various applications such as passwords, usernames, and product codes.

Steps to Check if the String is Alphanumeric

In Golang, we can check if a string is alphanumeric by using the regular expressions package. Here are the steps to check if the string is alphanumeric.

Step 1: Import the Regular Expressions Package

To use regular expressions in Golang, we need to import the "regexp" package.

import "regexp"

Step 2: Compile the Regular Expression

After importing the "regexp" package, we need to compile the regular expression pattern that matches alphanumeric strings. Here's how we can compile the regular expression pattern.

var alphanumeric = regexp.MustCompile("^[a-zA-Z0-9_]*$")

In the above code, we have defined a regular expression pattern that matches any string that contains alphabets, numbers, and underscores.

Step 3: Check if the String is Alphanumeric

Now that we have compiled the regular expression pattern, we can use the "MatchString" function to check if a string is alphanumeric or not. Here's how we can check if a string is alphanumeric.

func isAlphanumeric(str string) bool {
   return alphanumeric.MatchString(str)
}

In the above code, we have defined a function called "isAlphanumeric" that takes a string as an argument and returns a boolean value. The function uses the "MatchString" function to check if the string is alphanumeric or not.

Step 4: Test the Function

We can test the "isAlphanumeric" function with different input strings to check if it is working correctly. Here's how we can test the function.

func main() {
   str1 := "Hello123"
   str2 := "Hello@123"
   fmt.Println(isAlphanumeric(str1)) // Output: true
   fmt.Println(isAlphanumeric(str2)) // Output: false
}

In the above code, we have defined two input strings and passed them to the "isAlphanumeric" function. The function returns "true" for the first string, which is alphanumeric, and "false" for the second string, which contains a special character.

Example

package main

import (
   "fmt"
   "regexp"
)

func isAlphanumeric(str string) bool {
   var alphanumeric = regexp.MustCompile("^[a-zA-Z0-9_]*$")
   return alphanumeric.MatchString(str)
}

func main() {
   str1 := "Hello123"
   str2 := "Hello@123"
   fmt.Println(isAlphanumeric(str1)) // Output: true
   fmt.Println(isAlphanumeric(str2)) // Output: false
}

Output

true
false

Conclusion

In this article, we have discussed how to write a Golang program to check if a string is alphanumeric. We have used the regular expressions package to compile a regular expression pattern that matches alphanumeric strings and then used the "MatchString" function to check if a string is alphanumeric or not. By following the above steps, you can easily check if a string is alphanumeric in Golang.

Updated on: 19-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements