M
M
Muxauko2020-04-26 08:31:14
go
Muxauko, 2020-04-26 08:31:14

How to check a string for special characters?

It is necessary to check the string for the presence of special characters and numbers. Check if the string contains only letters of the Latin alphabet.
For example, the string "roi134432-=-!"#;;" is not suitable, but the string "golang" is.
Python has a wonderful method that literally in a couple of lines of code does this check, do we have something magical?))) )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Papa, 2020-04-26
@Muxauko

package main

import "fmt"
import "regexp"

var IsLetter = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString

func main() {
    fmt.Println(IsLetter("golang")) // true
    fmt.Println(IsLetter("рои134432-=-!№;;")) // false
}

https://play.golang.org/p/7HTIbsBjumg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question