Golang 如何检查一个字符是否为字母

Golang 如何检查一个字符是否为字母

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

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

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

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

大写字母- 65至90

小写字母- 97至122

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

方法1:使用isLetter()函数

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

语法

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

Func isLetter(r rune) bool

步骤

  • 第一步 - 初始化字符串。

  • 第二步 - 在字符串上运行一个for循环。

  • 第三步 - 在if条件中启动一个if条件,并调用IsLetter()函数。

  • 第四步 - 根据情况打印结果。

示例

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.

方法2:使用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的内容,您可以查看这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程