strings.ToUpper() method in Golang

ToUpper() method is used to convert the Unicode letters of a string to their uppercase values. In this post, we are going to discuss the ToUpper() method of the strings package in detail.

  • Method declaration – func ToUpper(s string) string
  • What does it do? – It will convert the Unicode letters of a string to their uppercase values. It will check whether a Unicode letter is within the range of ‘a’-‘z’ or not, and if it is within the range, then that letter will be mapped to its corresponding uppercase value, otherwise, it will be ignored.
  • What does it return? It will return a string after mapping all of its Unicode letters to their uppercase values.
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := "**Hi CODEKRU**"
	string2 := "LeaRnINg GoLang"
	string3 := "UPPERCASE string"

	fmt.Println("Original Strings: ")
	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)

	string1 = strings.ToUpper(string1)
	string2 = strings.ToUpper(string2)
	string3 = strings.ToUpper(string3)

	fmt.Println()
	fmt.Println("After using ToUpper() method: ")
	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)

}

Output –

Original Strings:
**Hi CODEKRU**
LeaRnINg GoLang
UPPERCASE string

After using ToUpper() method:
**HI CODEKRU**
LEARNING GOLANG
UPPERCASE STRING
What if we passed an uninitialized string in ToUpper() method argument?

An uninitialized string is considered an empty string in the Go language. So, it will be like passing the empty string to the ToUpper() function argument and thus we will get an empty string back.

package main

import (
	"fmt"
	"strings"
)

func main() {

	var string1 string

	fmt.Println("uppercase string:", strings.ToUpper(string1))

}

Output –

uppercase string: 

Here, nothing is printed because the string is empty.

If you want to convert your string into the lowercase one, then you can use the ToLower() method of the strings package.

Please visit this link if you want to know more about the Strings in Go language and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *