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 string, you are allowed to convert a string in the uppercase using the following functions. All these functions are defined under strings package, so you have to import strings package in your program to access these functions:
1. ToUpper: This function is used to convert the given string elements to uppercase. Or in other words, this function returns a copy of the given string in which all the Unicode characters are mapped into uppercase.
Syntax:
func ToUpper(str string) string
Here, str represents the string which you want to convert to uppercase. Let us discuss this concept with the help of an example:
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "WelcomE, GeeksforGeeks**"
str2 := "$$This is the, tuTorial oF Golang##"
str3 := "HELLO! GOLANG"
str4 := "uppercase conversion"
fmt.Println( "Strings (before):" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2:" , str2)
fmt.Println( "String 3:" , str3)
fmt.Println( "String 4:" , str4)
res1 := strings.ToUpper(str1)
res2 := strings.ToUpper(str2)
res3 := strings.ToUpper(str3)
res4 := strings.ToUpper(str4)
fmt.Println( "\nStrings (after):" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2:" , res2)
fmt.Println( "Result 3:" , res3)
fmt.Println( "Result 4:" , res4)
}
|
Output:
Strings (before):
String 1: WelcomE, GeeksforGeeks**
String 2: $$This is the, tuTorial oF Golang##
String 3: HELLO! GOLANG
String 4: uppercase conversion
Strings (after):
Result 1: WELCOME, GEEKSFORGEEKS**
Result 2: $$THIS IS THE, TUTORIAL OF GOLANG##
Result 3: HELLO! GOLANG
Result 4: UPPERCASE CONVERSION
2. ToTitle: This function is used to convert the string elements to title case. Or in other words, this function returns a copy of the given string in which all the Unicode characters are mapped into title case.
Syntax:
func ToTitle(str string) string
Here, str represents the string which you want to convert to title case. Let us discuss this concept with the help of an example:
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "WelcomE, GeeksforGeeks**"
str2 := "$$This is the, tuTorial oF Golang##"
str3 := "HELLO! GOLANG"
str4 := "uppercase conversion"
fmt.Println( "Strings (before):" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2:" , str2)
fmt.Println( "String 3:" , str3)
fmt.Println( "String 4:" , str4)
res1 := strings.ToTitle(str1)
res2 := strings.ToTitle(str2)
res3 := strings.ToTitle(str3)
res4 := strings.ToTitle(str4)
fmt.Println( "\nStrings (after):" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2:" , res2)
fmt.Println( "Result 3:" , res3)
fmt.Println( "Result 4:" , res4)
}
|
Output:
Strings (before):
String 1: WelcomE, GeeksforGeeks**
String 2: $$This is the, tuTorial oF Golang##
String 3: HELLO! GOLANG
String 4: uppercase conversion
Strings (after):
Result 1: WELCOME, GEEKSFORGEEKS**
Result 2: $$THIS IS THE, TUTORIAL OF GOLANG##
Result 3: HELLO! GOLANG
Result 4: UPPERCASE CONVERSION