How to check string contains alphabetic char or not in Golang
- Admin
- Dec 31, 2023
- Golang-examples
This tutorial explains how to Check Given string contains Alphabetic Characters or not
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
toz
and uppercase -A
toZ
.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.
- Find String contains alphabetic
- Check String contains alphabetic using a regular expression
- Check String contains alphanumeric and special characters
How to check if a string contains alphabetic characters in Golang?
String
characters are iterated using the range
loop. During each iteration, Check each character for lowercase
and uppercase
values and return true
for Alphabetic
and false
for not alphabetic.
Here is a code to check if the string contains an alphabet
package main
import (
"fmt"
)
// Function to check given string contains alphabet characters
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
How to check if a string contains alphabetic characters using Golang 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 or a string.
The first regular expression pattern is compiled using the MustCompile
function and returns an object. MatchString
function is used to check matched strings, and Regular Expression is used as regexp.
MustCompile(\`^\[a-zA-Z\]+$\`)
Following regular expression pattern are used
^
- Beginning of a string\[
- Character group startinga-z
- lowercase letterA-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
How to Check if String contains Alphanumeric characters or special characters in Golang
Regular expression pattern is ^\[a-zA-Z0-9\_\]\*$
.
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.