3 Different Ways to Capitalize First Letter in Golang

To capitalize the first letter of a string in Golang, you can use the following methods:

  1. Using strings.Title(): The strings.Title() function capitalizes the first letter of each word in a string. If you only need to capitalize the first letter of a string, you can use this method and then concatenate the rest of the string.
  2. Using a byte slice: You can convert the string into a byte slice, modify the first byte to be uppercase, and then convert it back to a string to capitalize the first letter.
  3. Using the Unicode package: You can use the unicode.ToUpper() function from the unicode package to convert the first letter of a string to uppercase.

Method 1: Using strings.Title() method

Golang strings.Title() capitalizes the first letter of each word in a given string. It returns a new string with all the first letters of words in the input string capitalized and all other characters in lowercase.

package main

import (
  "fmt"
  "strings"
)

func main() {
  str := "We are the world"
  title := strings.Title(str)
  fmt.Println(title)
}

Output

We Are The World

The strings.Title() does not modify the original string but returns a new string with the capitalized first letter of each word.

Method 2: Using a byte slice

You can convert the string into a byte slice, modify the first byte to be uppercase, and then convert it back to a string to capitalize the first letter.

package main

import (
  "fmt"
)

func capitalizeByteSlice(str string) string {
  bs := []byte(str)
  if len(bs) == 0 {
    return ""
  }
  bs[0] = byte(bs[0] - 32)
  return string(bs)
}

func main() {
  str := "hello"
  fmt.Println(capitalizeByteSlice(str))
}

Output

Hello

In this example, the capitalizeByteSlice() function takes a string as input and converts it to a byte slice.

If the length of the byte slice is 0, it returns an empty string. Otherwise, it changes the first byte’s value to its uppercase equivalent using the ASCII code.

Finally, it returns the modified string in which the first letter is capitalized.

Method 3: Using unicode.ToUpper()

You can use the unicode.ToUpper() function in Golang to convert the first letter of a string to uppercase.

package main

import (
  "fmt"
  "unicode"
)

func capitalize(str string) string {
  runes := []rune(str)
  runes[0] = unicode.ToUpper(runes[0])
  return string(runes)
}

func main() {
  word := "we are the world"
  fmt.Println(capitalize(word))
}

Output

We are the world

In this program, we capitalized the first letter of the input string word using the unicode.ToUpper() method.

The unicode.ToUpper() function takes a rune as input and returns the uppercase equivalent of the input rune if it is a lowercase letter. The resulting string is then returned and printed.

Conclusion

Use the strings.Title() method to convert every word’s first letter to be capitalized in Go.

Use the unicode.ToUpper() method to convert the first letter of the word of the sentence in Golang.

Leave a Comment