Go Program To Check String Contains Alphabetic,Numeric and Special Characters

Go Program to check string contains Alphabetic, numeric and special characters. In this program we will learn how to check string contains Alphabetic, numeric and special characters. alphabetic characters – each character contains a letter from lowercase.

Go Program To Check String Contains Alphabetic,Numeric and Special Characters

Example 1: To check if a string contains alphabetic characters

Check each character for lowercase and uppercase values and return true for Alphabetic and false – not alphabetic

Source Code

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("Insheera")) // true
 fmt.Println(checkStringAlphabet("4286"))   // false

}

Output

Example 2: To check if a string contains alphabetic characters using Regular Expression

Golang using regex standard inbuilt package. This includes regular expressions and pattern matching First regular expression pattern is compiled using MustCompile function and returns an object. with this object, MatchString is used to check matched string Regular Expression used as regexp

Source Code

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("Insheera")) // true
 fmt.Println(checkStringAlphabet("4286"))   // false

}

Output