How to convert a string in lower case in Golang?


In Golang, a string is a sequence of Unicode characters. Sometimes, you may want to convert a string to lowercase, which means converting all the letters to their lowercase equivalents. This can be achieved using various methods in Golang. In this article, we will learn how to convert a string to lowercase using different approaches.

Using strings.ToLower()

The easiest and most common method to convert a string to lowercase in Golang is by using the strings.ToLower() function. This function converts all the ASCII letters in a string to their lowercase equivalents. Here's how to use it to convert a string to lowercase −

Example

Here's an example −

package main

import (
   "fmt"
   "strings"
)

func main() {
   s := "HELLO WORLD"
   fmt.Println("Original:", s) // Output: Original: HELLO WORLD
   s = strings.ToLower(s)
   fmt.Println("Lowercase:", s) // Output: Lowercase: hello world
}

Output

Original: HELLO WORLD
Lowercase: hello world

In this example, we create a string s with the value "HELLO WORLD". We then pass the string to the strings.ToLower() function to convert it to lowercase. The strings.ToLower() function returns a new string with all the ASCII letters in lowercase. We then assign the new string back to s and print the original and lowercase versions of the string using the fmt.Println() function.

Using strings.Map()

Another way to convert a string to lowercase in Golang is by using the strings.Map() function. The strings.Map() function iterates over each character in a string and applies a given function to it. Here's an example of how to use strings.Map() to convert a string to lowercase −

Example

package main

import (
   "fmt"
   "strings"
   "unicode"
)

func main() {
   s := "HELLO WORLD"
   fmt.Println("Original:", s) // Output: Original: HELLO WORLD
   s = strings.Map(unicode.ToLower, s)
   fmt.Println("Lowercase:", s) // Output: Lowercase: hello world
}

Output

Original: HELLO WORLD
Lowercase: hello world

In this example, we create a string s with the value "HELLO WORLD". We then pass the string to the strings.Map() function along with the unicode.ToLower function. The unicode.ToLower function converts a character to its lowercase equivalent. The strings.Map() function iterates over each character in the string and applies the unicode.ToLower function to it. The function returns a new string with all the characters in lowercase. We then assign the new string back to s and print the original and lowercase versions of the string using the fmt.Println() function.

Using a Loop

You can also convert a string to lowercase in Golang by iterating over each character in the string and converting it to its lowercase equivalent. Here's an example of how to use a loop to convert a string to lowercase −

Example

package main

import (
   "fmt"
)

func toLowerCase(s string) string {
   var result string
   for _, char := range s {
      if char >= 'A' && char <= 'Z' {
         result += string(char + 32)
      } else {
         result += string(char)
      }
   }
   return result
}

func main() {
   s := "HELLO WORLD"
   fmt.Println("Original:", s) // Output: Original: HELLO WORLD
   s = toLowerCase(s)
   fmt.Println("Lowercase:", s) // Output: Lowercase: hello world
}

Output

Original: HELLO WORLD
Lowercase: hello world

The function iterates over the characters of the input string and checks whether each character is within the range of uppercase ASCII characters. If so, it adds the corresponding lowercase character to the output string. Otherwise, it adds the original character to the output string. The main function demonstrates the use of the toLowerCase function by calling it with a sample string and printing the original and lowercase versions of the string to the console.

Conclusion

Converting a string to lowercase in Golang can be achieved using various approaches. The most common method is to use the strings.ToLower() function, which converts all the ASCII letters in a string to their lowercase equivalents. Another way is to use the strings.Map() function, which iterates over each character in a string and applies a given function to it. Finally, you can also use a loop to convert a string to lowercase by iterating over each character in the string and converting it to its lowercase equivalent. All these approaches return a new string with all the characters in lowercase.

Updated on: 20-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements