如何在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.