How to convert a string in lower case in Golang?
Last Updated : 12 Nov, 2024
In Go, converting a string to lowercase is a common operation that can be accomplished easily using the strings
package. This package provides various string manipulation functions, including the ToLower
function, which converts all Unicode characters in a string to their lowercase equivalent.
Example
Input: Hello, Geeks!
Output: hello, geeks!
Explanation: The ToLower
function in Go converts all Unicode characters in a string to their lowercase equivalent, returning a new string.
Syntax
The syntax of the ToLower
function is as follows:
lowerS := strings.ToLower(s)
Where:
s
is the original string you want to convert to lowercase.lowerS
is the new string with all characters converted to lowercase.
Convert a String to Lower Case
In Go, converting a string to lowercase is a common operation that can be accomplished easily using the ToLower
function.
Syntax
The syntax of the ToLower function is as follows:
lowerS := strings.ToLower(s)
Example
Go package main import ( "fmt" "strings" ) func main() { // Initial string s := "Hello, Geeks!" fmt.Println("", s) // Converting to lower case lowerS := strings.ToLower(s) fmt.Println("", lowerS) }
Output Hello, Geeks! hello, geeks!
Converting Multiple Strings
You can also convert multiple strings to lower case by using a loop.
Syntax
for i, s := range phrases {
phrases[i] = strings.ToLower(s)
}
Where:
phrases
is a slice of strings that you want to convert to lower case.
Example
Go package main import ( "fmt" "strings" ) func main() { // Initial strings phrases := []string{"Hello, Geeks!", "GoLang is Fun!"} fmt.Println("", phrases) // Converting to lower case for i, s := range phrases { phrases[i] = strings.ToLower(s) } fmt.Println("", phrases) }
Output [Hello, Geeks! GoLang is Fun!] [hello, geeks! golang is fun!]
Handling User Input
You can also convert user input to lower case.
Syntax
fmt.Scanln(&userInput)
lowerInput := strings.ToLower(userInput)
Where:
userInput
captures the string entered by the user.
Example
Go package main import ( "fmt" "strings" ) func main() { // User input var userInput string fmt.Print("Enter string: ") fmt.Scanln(&userInput) // Converting to lower case lowerInput := strings.ToLower(userInput) fmt.Println("Lower case input:", lowerInput) }
OutputEnter string: Lower case input:
Conclusion
Converting a string to lower case in Go is straightforward using the strings.ToLower
function. Whether working with a single string, multiple strings, or user input, the process remains simple and efficient.
Similar Reads
How to convert a string in uppercase in Golang? In Go, strings are sequences of variable-width characters represented using UTF-8 Encoding. You can convert a string to uppercase using functions from the strings package, which must be imported into your program. In this article we will learn "How to Convert a String to Uppercase in Golang".Example
2 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read
How to Get the String in Specified Base in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
2 min read
How to Replace Characters in Golang String? In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to replace characters in the given string usin
4 min read
How to Reverse a String in Golang? Given a string and the task is to reverse the string. Here are a few examples. Approach 1: Reverse the string by swapping the letters, like first with last and second with second last and so on. Example: C // Golang program to reverse a string package main // importing fmt import "fmt" // function,
2 min read