GoLang String - ToLower() and ToUpper()

In this tutorial, we will learn how to convert the entire string to lowercase and how to convert the entire string to uppercase using in-built methods.

In the Go Language, If you want to convert the entire string to lowercase then you should know about ToLower() method. If you want to convert all characters in the string to uppercase, ToUpper() method will be used. let us explore the uses cases of each separately with help of simple examples.

String ToLower()

ToLower() is used to convert all characters in the given string to lower case.

Syntax:

strings.ToLower(actual_string)

It takes only one parameter.

Parameter:

actual_string is the string to be converted to lower case.

It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "WELCOME TO GKINDEX" and convert it to lowercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "WELCOME TO GKINDEX"
 
    fmt.Println("String: ", actual_string)

      // Convert the string to lowercase.
     fmt.Println( "Lowercase: ",strings.ToLower(actual_string))
     
}

Output:

CopiedCopy Code

String:  WELCOME TO GKINDEX
Lowercase:  welcome to gkindex

Example 2:

Let us consider the string - "welcome to gkindex" and convert it to lowercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Convert the string to lowercase.
     fmt.Println( "Lowercase: ",strings.ToLower(actual_string))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
Lowercase:  welcome to gkindex

String ToUpper()

ToUpper() is used to convert all characters in the given string to upper case.

Syntax:

strings.ToUpper(actual_string) It takes only one parameter.

Parameter:

actual_string is the string to be converted to upper case.

It is important to specify the "strings" package in import.

Example 1: -

Let us consider the string - "welcome to gkindex" and convert it to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)

      // Convert the string to Uppercase.
     fmt.Println( "Uppercase: ",strings.ToUpper(actual_string))
     
}

Output:

CopiedCopy Code

String:  welcome to gkindex
Uppercase:  WELCOME TO GKINDEX

Example 2: -

Let us consider the string - "WELCOME TO GKINDEX" and convert it to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "WELCOME TO GKINDEX"
 
    fmt.Println("String: ", actual_string)

      // Convert the string to Uppercase.
     fmt.Println( "Uppercase: ",strings.ToUpper(actual_string))
     
}

Output:

CopiedCopy Code

String:  WELCOME TO GKINDEX
Uppercase:  WELCOME TO GKINDEX

Conclusion

Now we know how convert string to lowercase and uppercase in a Golang using ToLower() and ToUpper() functions and understood the implementation with working examples.