Go Example - Program to check string contains Alphabetic,numeric and special characters


Golang String characters

The string is a group of characters enclosed in double quotes.

alphabetic characters - each character contains a letter from lowercase - a to z and uppercase - A to Z.
alphanumeric characters - contain alphabetic and numeric characters.
Special characters - a character are like %,# ..etc with excluding alphabetic and numeric characters.

This post, You will learn the following things in GoLang

  • check String contains alphabetic
  • check String contains alphabetic using a regular expression
  • check String contains alphanumeric and special characters

Example 1: How to check if a string contains alphabetic characters 

String characters are iterated using for each loop. Check each character for lowercase and uppercase values and return true for Alphabetic and false - not alphabetic

package main  
  
import (  
 "fmt"  
)  
  
func checkStringAlphabet(str string) bool {  
 for _, charVariable := range str {  
  if (charVariable < 'a' || charVariable > 'z') && (charVariable < 'A' || charVariable > 'Z') {  
   return false  
  }  
 }  
 return true  
}  
  
func main() {  
 fmt.Println(checkStringAlphabet("Kiran")) // true  
 fmt.Println(checkStringAlphabet("123"))   // false  
  
}  

Output is

true  
false  

Example 2: How to check if a string contains alphabetic characters using regular Expression

Regular expressions support avialable in Golang using regex standard inbuilt package. This includes regular expressions and pattern matching First regular expression pattern is compiled using MustCompile function and returns object. with this object, MatchString is used to check matched string Regular Expression used as regexp.MustCompile(`^[a-zA-Z]+$`)
^ - Beginning of a string
[ - Character group starting
a-z - lower case letter
A-Z - upper case letter
] - Character group end
$ - String ending
+ - one or more characters
Here is a Program to check String contains Alphabetic using a regular expression

package main  
  
import (  
 "fmt"  
 "regexp"  
)  
  
var isStringAlphabetic = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString  
  
func main() {  
 fmt.Println(isStringAlphabetic("kiran")) // true  
 fmt.Println(isStringAlphabetic("a1a"))   // false  
}  

Output is

true  
false  

Example 3- Check String contains Alphanumeric characters or special characters

Regular expression patter used is ^[a-zA-Z0-9_]*$. which returns true for alphabetic, numeric and underscore characters, return false - for special characters Here is a program to check a string for Alphabetic and special characters

package main  
  
import (  
 "fmt"  
 "regexp"  
)  
  
var isStringAlphabetic = regexp.MustCompile(`^[a-zA-Z0-9_]*$`).MatchString  
  
func main() {  
 fmt.Println(isStringAlphabetic("cloudhadoop")) // true  
 fmt.Println(isStringAlphabetic("abc123"))      // true  
 fmt.Println(isStringAlphabetic("abc_"))        // true  
 fmt.Println(isStringAlphabetic("abc|}"))       // false  
 fmt.Println(isStringAlphabetic("%^&abc"))      // false  
  
}  

When you compiled and executed, the output is

true  
true  
true  
false  
false  

Similar Posts