Open In App

Using Title Method to Capitalise String in Golang

Improve
Improve
Like Article
Like
Save
Share
Report

The strings.Title method is being deprecated from Golang, so we need an alternative to make our strings Title cased or Capital Case. The text/cases and the text/language package is the recommended way to make the strings Title cased in Golang. In this article, we will be understanding how to use the Title method in the text/cases package.

Approach

The Title method is provided by the text/cases package which helps in creating a Caser object. This caser object can further be used on strings to format or perform operations on the strings and return the resultant string.

So, by using the Title method, we will be creating the Caser object which acts as an object for performing the desired options in this case we are Title casing the string. The Title method takes in parameters like language type which can be passed to filter the string as per the semantics of that particular language. So, with this method, we have a wide variety of options of languages to choose from. 

Further, we have to call the Caser object with certain methods on the strings that can be used to evaluate the results in a particular format. We can call the associated methods on the Caser object to evaluate a string, upper case, etc. Thereby we will get the resultant string in the title cased form. 

So to do the above task follow the following points step-by-step:

Step 1 – Installing packages

Let’s first initialize a go project. We can simply create an empty file and you can name it whatever you like, I will be naming it main.go. We then can initialize the go mod which is a package management tool for the Golang projects. By the go mod init command. We will have the go project initialized.

go mod init <name>

 

So, after initializing the go project, we can install the packages which are text/cases and text/language. We can install it with the go get command from the command line. 

go get golang.org/x/text/cases

 

go get golang.org/x/text/language

 

So, now we have the text/cases and text/language package installed inside the go project. We will now have access to these packages from the scripts. Now, we can move ahead into the scripting part of the Title casing a string with the help of the Title method from these packages.

Step 2 – Setting up the Title method’s parameters

To work with the cases, we need to create a Caser object first, we can’t directly use the Title method as it returns the Caser-type object instead of a string. So, that is why we need to create the type of Caser and then further process the Caser object to our desires. For calling the Title method we require one parameter which is the language type. It can be any one of the mentioned lists of the language type.

Go




// Go program to illustrate Title method to capitalise string
package main
 
import (
    "fmt"
 
    "golang.org/x/text/cases"
    "golang.org/x/text/language"
)
 
func main() {
    str := "peter parker. james bond"
    caser := cases.Title(language.English)
}


So, here we have defined the string with some words and they form kind of two English sentences due to the full stop at the end of the second word. So that I the string which we want to make into the title case.

We then define the variable caser which will be used for title casing the string. But we have to first create a kind of object which we can work on later for converting it into a string. We call the function Title from the cases package, as said, the Title method takes in one parameter and one optional parameter. The first parameter is the language Type. We have set the language type from the language package and accessed the English type from it. This will create a caser object which if we try to print will look like a memory address. 

go run main.go

 

So, we know the object is not of type string, we need to call certain methods for being able to cast into a string representation of the data. If we see the source code of the caser object, it returns a type of transformer. The transformer object is an actual underlying object which will convert the string into a title case. Since we have used the title method to the cases type object, we will get the transformed to perform a transformation to the parsed string.

Step 3 – Using case object to title case string

Since we have the caser object we can call a few methods on it for returning the type as a string. We have the method available in the cases package applicable to the caser type object called String which will give out the string representation of the transformed string passed to the function.

Go




// Go program to illustrate Title method to capitalise string
package main
 
import (
    "fmt"
 
    "golang.org/x/text/cases"
    "golang.org/x/text/language"
)
 
func main() {
    str := "peter parker. james bond"
    caser := cases.Title(language.English)
    fmt.Println("str = ", str)
    str_title := caser.String(str)
    fmt.Println("str_title_cased = ", str_title)
}


 

Here, we can see that the string has been successfully transformed into the title case. The String method in the caser type object takes a parameter string which applies the transformation according to the caser type object and returns a string result. So, by using the caser object and applying the title transformation to it, we have returned a string representation of the title-cased transformation object.



Last Updated : 13 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads