GoLang String - Title() and ToTitle()

In this tutorial, we will learn how to capitalize only first word in a string using Title() method and will understand to make all uppercase using ToTitle().

In the Go Language, If you want to capitalize only first word in a string, then you should know about Title() method. If you want to convert all characters in the string to uppercase, ToTitle() method will be used. let us explore the uses cases of each separately with help of simple examples.

String Title()

Title() is the method available in strings package which is used to convert every first character of the word to uppercase in the actual_string. If all the characters in the string is in uppercase, then the same string is returned.

Syntax:


strings.Title(actual_string)

It will take string as parameter. It is important to specify the "strings" package in import.

Example 1:

Let's consider the string - "welcome to gkindex" and use Title() function to convert first character in each word in the string to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)
fmt.Println( strings.Title(actual_string))
}

Output:

CopiedCopy Code

String:  welcome to gkindex
Welcome To Gkindex

So, there are three words in the actual_string. For each word first character is converted to upper case.

Example 2:

Let's consider the string - "WELCOME TO GKINDEX" and use Title() function to convert first character in each word in the string to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "WELCOME TO GKINDEX"
 
    fmt.Println("String: ", actual_string)
fmt.Println( strings.Title(actual_string))
}

Output:

CopiedCopy Code

String:  WELCOME TO GKINDEX
WELCOME TO GKINDEX

So, there are three words in the actual_string and the string is already in upper case. So, the same string is returned.

Example 3:

Let's consider the string - "wELCOME tO gkINDEX Go language" and use Title() function to convert first character in each word in the string to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "wELCOME tO gkINDEX Go language"
 
    fmt.Println("String: ", actual_string)
fmt.Println( strings.Title(actual_string))
}

Output:

CopiedCopy Code

String:  wELCOME tO gkINDEX Go language
WELCOME TO GkINDEX Go Language

So, there are five words in the actual_string. For each word first character is converted to upper case.

String ToTitle()

ToTitle() is the method available in strings package which is used to convert all characters in the string to uppercase.

Syntax:


strings.ToTitle(actual_string)

It will take string as parameter.

It is important to specify the "strings" package in import.

Example 1:

Let's consider the string - "welcome to gkindex" and use ToTitle() function to convert the entire string into uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "welcome to gkindex"
 
    fmt.Println("String: ", actual_string)
fmt.Println( strings.ToTitle(actual_string))
}

Output:

CopiedCopy Code

String:  welcome to gkindex
WELCOME TO GKINDEX

So, there are three words in the actual_string. All are converted to uppercase.

Example 2:

Let's consider the string - "WELCOME TO GKINDEX" and use ToTitle() function to convert the string to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "WELCOME TO GKINDEX"
 
    fmt.Println("String: ", actual_string)
fmt.Println( strings.ToTitle(actual_string))
}

Output:

CopiedCopy Code

String:  WELCOME TO GKINDEX
WELCOME TO GKINDEX

So, there are three words in the actual_string and the string is already in upper case. So, the same string is returned.

Example 3:

Let's consider the string - "wELCOME tO gkINDEX Go language" and use ToTitle() function to all characters in the string to uppercase.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "wELCOME tO gkINDEX Go language"
 
    fmt.Println("String: ", actual_string)
fmt.Println( strings.ToTitle(actual_string))
}

Output:

CopiedCopy Code

String:  wELCOME tO gkINDEX Go language
WELCOME TO GKINDEX GO LANGUAGE

So, there are five words in the actual_string. All are converted into uppercase.

Conclusion

Title() in Golang will convert every first character of the word to uppercase in the actual_string whereas, ToTitle() convert all characters in the string to uppercase. We discuss both the methods with examples for the better understanding