TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang ToLower, ToUpper String Examples

Use the strings.ToLower and ToUpper funcs to lowercase and uppercase strings.
ToLower, ToUpper. There are lowercase and uppercase letters. To change from one to the other, we can use functions from the strings package in Go.Strings
Simple methods. The strings.ToLower and ToUpper funcs have the expected results. They do not change characters like digits, spaces, or punctuation. Title() capitalizes all words.
ToLower example. Let us begin with strings.ToLower. We have a string literal with the value of "Name." When we call ToLower, a new string is returned.

And: The uppercase character in the string was changed to a lowercase one—but only in the copied, returned string.

Tip: The original string (value) is left alone after strings.ToLower returns. This is a behavior shared by many modern languages.

Golang program that uses ToLower package main import ( "fmt" "strings" ) func main() { value := "Name" // convertolowercase valueLower := strings.ToLower(value) fmt.Println("BEFORE:", value) fmt.Println("AFTER: ", valueLower) } Output BEFORE: Name AFTER: name
ToUpper example. The ToUpper func works in the same way as ToLower. It returns a new, copied version of the string argument that has lowercase chars changed to uppercase ones.
Golang program that uses ToUpper package main import ( "fmt" "strings" ) func main() { value := "The Dev Codes" // Use ToUpper. valueUpper := strings.ToUpper(value) fmt.Println("BEFORE:", value) fmt.Println("AFTER: ", valueUpper) } Output BEFORE: The Dev Codes AFTER: DOT NET PERLS
Title. The strings package provides ways to change the case of characters. Here we apply title-casing to a string with the Title func. The result is a properly-capitalized string.
Golang program that uses Title func package main import ( "fmt" "strings" ) func main() { name := "the apology" // Capitalize this string. result := strings.Title(name) fmt.Println(result) } Output The Apology
A summary. For real-world programs in Go, we often need methods like strings.ToLower and ToUpper. These can help us simplify our programs (as by using only lowercase-based logic).
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf