Check If the Rune is an Uppercase Letter or not in Golang


A rune in Golang is a representation of a Unicode code point, which is an integer number used to identify a particular character. In many programmes, it's necessary to determine whether a rune is an uppercase letter or not, and Golang has built-in functions to help with this. This article will explain how to use examples to show how to use the Golang check to see if a rune is an uppercase letter.

Checking If a Rune is an Uppercase Letter

In Golang, the built-in unicode package provides the IsUpper() function that can be used to check if a rune is an uppercase letter. This function takes a single argument of type rune and returns a boolean value indicating whether the rune is an uppercase letter or not. Here is an example −

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   r1 := 'A'
   r2 := 'b'
   r3 := '1'

   fmt.Println(unicode.IsUpper(r1)) // true
   fmt.Println(unicode.IsUpper(r2)) // false
   fmt.Println(unicode.IsUpper(r3)) // false
}

Output

true
false
false

In the above example, we use the IsUpper() function to check if the runes r1, r2, and r3 are uppercase letters. The output of the program is true, false, and false, respectively.

Using If Statement to Check If a Rune is an Uppercase Letter

We can also use an if statement to check if a rune is an uppercase letter. Here is an example −

Example

package main

import (
   "fmt"
)

func main() {
   r := 'B'

   if r >= 'A' && r <= 'Z' {
      fmt.Println("The rune is an uppercase letter.")
   } else {
      fmt.Println("The rune is not an uppercase letter.")
   }
}

Output

The rune is an uppercase letter.

In the above example, we use an if statement to check if the rune r is an uppercase letter. If the rune is an uppercase letter, the program prints "The rune is an uppercase letter." Otherwise, it prints "The rune is not an uppercase letter."

Conclusion

The method for determining whether a rune is an uppercase letter in Golang was covered in this article. Golang has an inbuilt function called unicode, as we have seen. For this evaluation, use IsUpper(). We've also demonstrated how to use an if statement to see if a rune is an uppercase letter. These techniques make it simple for our Golang applications to determine whether a rune is an uppercase letter.

Updated on: 07-Apr-2023

244 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements