📜  在 Golang 中检查符文是否是字母

📅  最后修改于: 2021-10-25 02:06:26             🧑  作者: Mango

Rune 是 ASCII 的超集或者是 int32 的别名。它包含世界书写系统中可用的所有字符,包括重音符号和其他变音符号、制表符和回车符等控制代码,并为每个字符分配一个标准编号。这个标准数字在 Go 语言中被称为 Unicode 代码点或符文。
您可以在IsLetter()函数的帮助下检查给定的符文是否为字母。如果给定的符文是字母,则此函数返回 true,如果给定的符文不是字母,则返回 false。这个函数是在Unicode包下定义的,所以为了访问这个方法,你需要在你的程序中导入Unicode包。

句法:

func IsLetter(r rune) bool

此函数的返回类型是布尔值。让我们在给定的例子的帮助下讨论这个概念:

示例 1:

// Go program to illustrate how to
// check the given rune is a letter
package main
  
import (
    "fmt"
    "unicode"
)
  
// Main function
func main() {
  
    // Creating rune
    rune_1 := 'g'
    rune_2 := 'e'
    rune_3 := '1'
    rune_4 := '4'
    rune_5 := 'S'
  
    // Checking the given rune 
    // is a letter or not
    // Using IsLetter() function
    res_1 := unicode.IsLetter(rune_1)
    res_2 := unicode.IsLetter(rune_2)
    res_3 := unicode.IsLetter(rune_3)
    res_4 := unicode.IsLetter(rune_4)
    res_5 := unicode.IsLetter(rune_5)
  
    // Displaying results
    fmt.Println(res_1)
    fmt.Println(res_2)
    fmt.Println(res_3)
    fmt.Println(res_4)
    fmt.Println(res_5)
  
}

输出:

true
true
false
false
true

示例 2:

// Go program to illustrate how to
// check the given rune is a letter
package main
  
import (
    "fmt"
    "unicode"
)
  
// Main function
func main() {
  
    // Creating a slice of rune
    val := []rune{'g', 'E', '3', 'K', '1'}
  
    // Checking each element of the given 
    // slice of the rune is a letter or not
    // Using IsLetter() function
    for i := 0; i < len(val); i++ {
  
        if unicode.IsLetter(val[i]) == true {
  
            fmt.Println("It is a letter")
              
        } else {
          
            fmt.Println("It is not a letter")
        }
    }
}

输出:

It is a letter
It is a letter
It is not a letter
It is a letter
It is not a letter