如何在Go语言中检查字符是否属于字母?


在本教程中,我们将学习如何检查字符是否为字母。本教程包含两种实现方法:

首先,使用fmt库中内置的isLetter()函数可以减少代码行数。

另一种方法是使用ASCII值的概念,因为每个字符都有一个唯一的ASCII值,我们可以用它来判断当前字符是否为字母。

大写和小写字母的范围如下:

大写字母 – 65到90

小写字母 – 97到122

如果字符的ASCII值在上述范围内,则该字符为字母。

方法一:使用isLetter()函数

在这个例子中,我们将使用fmt库中内置的isLetter()函数来检查字符是否为字母。

语法

fmt库中isLetter()函数的语法如下。此函数接受一个字符作为参数。

Func isLetter(r rune) bool

算法

  • 步骤1 - 初始化字符字符串。

  • 步骤2 - 对字符串运行for循环。

  • 步骤3 - 开始if条件,并在if条件中调用IsLetter()函数。

  • 步骤4 - 据此打印结果。

示例

package main
import (
   
   // fmt package provides the function to print anything
   "fmt"
   
   // unicode function is providing isLetter function
   "unicode"
)
func main() {
   // declaring and initializing the variable using the shorthand method in Golang
   characters := "A&j()K"
   fmt.Println("Golang program to check the character is an alphabet or not using IsLetter() function present in the Unicode library.")
   
   // running a for loop to check each character in the string is alphabet or not
   for i := 0; i < len(characters); i++ {
      
      // calling the Isletter() function and printing the result on the basis
      // of return value of the function
      if unicode.IsLetter(rune(characters[i])) {
      fmt.Printf("%c is a character.\n", characters[i])
      } else {
         fmt.Printf("%c is not a character.\n", characters[i])
      }
   }
}

输出

% go run tutorialpoint.go
Golang program to check whether the character is an alphabet or not using the IsLetter() function present in the Unicode library.
A is a character.
& is not a character.
j is a character.
( is not a character.
) is not a character.
K is a character.

方法二:使用ASCII字符

在这个例子中,我们将使用ASCII字符范围来检查字符是否为字母。

语法

比较将使用如下语法所示的小写和大写字母的ASCII值范围进行。

if (characters[i] >= 65 && characters[i] <= 90) || (characters[i] >= 97 && characters[i] <= 122) { }

算法

  • 步骤1 - 初始化字符字符串。

  • 步骤2 - 对字符串运行for循环。

  • 步骤3 - 开始if条件,并比较当前索引字符的ASCII值是否在65到90或97到122之间。

  • 步骤4 - 据此打印结果。

示例

package main
import (

// fmt package provides the function to print anything
   "fmt"
)
func main() {

   // declaring and initializing the variable using the shorthand method in Golang 
   characters := "A&j()K"
   fmt.Println("Golang program to check whether the character is an alphabet or not using the concept of ASCII values.")
   
   // running a for loop to check if each character in the string is alphabet or not
   for i := 0; i < len(characters); i++ {
   
      // checking that the ASCII value of the character is in between the range
      // of uppercase or lowercase characters or not
      if (characters[i] >= 65 && characters[i] <= 90) || (characters[i] >= 97 && characters[i] <= 122) {
         fmt.Printf("%c is a character.\n", characters[i])
      } else {
         fmt.Printf("%c is not a character.\n", characters[i])
      }
   }
}

输出

% go run tutorialpoint.go
Golang program to check whether the character is an alphabet or not using the concept of ASCII values.
A is a character.
& is not a character.
j is a character.
( is not a character.
) is not a character.
K is a character.

结论

这是检查字符是否为字母的两种方法。第一种方法在模块化和代码可重用性方面更适用。要了解更多关于go的信息,您可以浏览这些教程。

更新于:2023年1月11日

888 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.