
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Lowercase in Golang
In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. In this article we will write a go language program to convert a string to lowercase.
Method 1: Using User-defined function
In this method, we are going to create a user-defined function to convert a given string into lowercase using in go programming language.
Algorithm
Step 1 ? First, we need to import the fmt package.
Step 2 ? Then, create a function to convert a string to lowercase. The function accepts the string as the argument. Convert the string obtained into byte variable and use a for loop to iterate over the byte.
Step 3 ? Now, if the characters forming the string are in lowercase then take that particular character of the byte and subtract from it the difference between a and A which turns out to be 32.
Step 4 ? After that subtract the difference from the values of the byte array and store them back to the byte array.
Step 5 ? Return the byte array. start the main() function. Inside the main() initialize a string and print it on the screen.
Step 6 ? Now call the ToLower() function and pass the string as argument to it. store the result obtained in a variable called result and print the result on the screen.
Example
In this example we will write a go language program to convert a string to lowercase by using an external function.
package main import ( "fmt" ) // function to convert characters to lower case func ToLower(s string) string { var lower string for _, c := range s { if c >= 'A' && c <= 'Z' { lower += string(c + 32) } else { lower += string(c) } } return lower } func main() { var input string = "THIS IS A STRING TO BE CONVERTED" fmt.Println("The given string is:\n", input) var output string = ToLower(input) fmt.Println() fmt.Println("String obtained by converting the above string to lowercase is:\n", output) }
Output
The given string is: THIS IS A STRING TO BE CONVERTED String obtained by converting the above string to lowercase is: this is a string to be converted
Method 2: Using internal function
In this method, we are going to use the internal ToLower() function of string and Unicode to convert a given string into lowercase in go programming language.
Syntax
func ToLower(r rune) rune
ToLower() function present in unicode package is used to convert a given string to lowercase. It takes the rune character as an argument and returns a rune in lowercase by converting the given rune to lowercase.
func ToLower(str string) string
ToLower() function is present in strings package and is used to convert a given string to lowercase. The function accepts the given string as argument and returns the final string after converting it to lowercase.
Algorithm
Step 1 ? First, we need to import the fmt and Unicode package.
Step 2 ? Now, create the ToLower() function that accepts the string whose case is to be changed and returns the string. Inside the function we are first converting the string obtained into rune and then using for loop to iterate over the rune array.
Step 3 ? If the character of the array turns out to be in lowercase then we need to convert it to lower case by using Unicode.Lower() function by passing that character as argument to it.
Step 4 ? Now, store the character one by one in the initialized array and return the final array.
Step 5 ? Now, start the main() function. Here initialize a variable of type string and store value to it. further print the array on the screen.
Step 6 ? Call the ToLower() function by passing the string as argument to it and store the result in a variable and print it on the screen.
Example 1
In this example we will the Unicode package to convert a given string to lowercase in go programming language
package main import ( "fmt" "unicode" ) func ToLower(s string) string { str := []rune(s) var lower string for _, c := range str { lower += string(unicode.ToLower(c)) } return string(lower) } func main() { var input string = "CONVERT IT TO LOWERCASE" fmt.Println("The given string is:\n", input) var output string = ToLower(input) fmt.Println() fmt.Println("The string obtained by converting above string to lowercase:\n", output) }
Output
The given string is: CONVERT IT TO LOWERCASE The string obtained by converting above string to lowercase: convert it to lowercase
Example 2
In this example we will write a go language program to convert a string to lowercase by using the ToLower() function present in strings package.
package main import ( "fmt" "strings" ) func main() { var input string = "CONVERT IT TO LOWERCASE" fmt.Println("The given string is:\n", input) var output string = strings.ToLower(input) fmt.Println() fmt.Println("The string obtained by converting the given string to lowercase is:\n", output) }
Output
The given string is: CONVERT IT TO LOWERCASE The string obtained by converting the given string to lowercase is: convert it to lowercase
Conclusion
We have successfully compiled and executed a go language program to convert a string to lowercase along with examples. we have used both for loops and library functions present in string as well as Unicode packages in order to obtain the required result.