{

Go Example-Program to check string contains alphabetic char


Golang Program to check string contains alphabetic char examples

Golang String characters Examples

The string is a group of characters enclosed in double-quotes. Characters can be any of the following types

  • 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 is like %,# ..etc excluding alphabetic and numeric characters.

In 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 each loop. During each iteration,Check each character for lowercase and uppercase values and return true for Alphabetic and false - not alphabetic

Here is a code to check if the string contains an alphabet

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:

true  
false  

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

Regular expressions are supported and available in Golang using the regex standard inbuilt package.

It includes checking regular expressions and pattern matching against a text.

The first regular expression pattern is compiled using the MustCompile function and returns an object. MatchString is used to check matched string, and Regular Expression is 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:

true  
false  

Example 3- Check if String contains Alphanumeric characters or special characters

Regular expression pattern is ^[a-zA-Z0-9_]*$ used.

It 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 compile and execute, the output is

true  
true  
true  
false  
false  

Conclusion

To summarize, You learned multiple programs to check strings containing Alphabetic, numeric and special characters in the Go language.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.