How to convert a slice of bytes in title case in Golang?


In Golang, a byte slice is a sequence of bytes. A slice of bytes can be created using the built-in function []byte(). Sometimes, you may want to convert a slice of bytes to title case, which means capitalizing the first letter of every word. This can be easily achieved using the strings.Title() function. In this article, we will learn how to convert a slice of bytes to title case in Golang.

Using strings.Title()

The strings.Title() function converts the first letter of each word in a string to uppercase. Here's how to use it to convert a slice of bytes to title case −

Example

package main

import (
   "fmt"
   "strings"
)

func main() {
   s := []byte("hello world")
   fmt.Println("Original:", string(s)) // Output: Original: hello world
   s = []byte(strings.Title(string(s)))
   fmt.Println("Title case:", string(s)) // Output: Title case: Hello World
}

Output

Original: hello world
Title case: Hello World

In this example, we create a slice of bytes s with the value "hello world". We then convert the slice to a string using the string() function and pass it to the strings.Title() function to convert it to title case. The strings.Title() function returns a new string with the first letter of each word capitalized. We then convert the new string back to a slice of bytes using the []byte() function and assign it back to s. Finally, we print the original and title case versions of the slice using the fmt.Println() function.

Using For Loop

If you prefer to convert a slice of bytes to title case without using the strings.Title() function, you can use a for loop and the unicode.ToTitle() function. Here's an example −

Example

package main
import (
   "fmt"
   "unicode"
)

func main() {
   s := []byte("hello world")
   fmt.Println("Original:", string(s)) // Output: Original: hello world
   inWord := true
   for i, b := range s {
      if unicode.IsSpace(rune(b)) {
         inWord = false
      } else if inWord {
         s[i] = byte(unicode.ToTitle(rune(b)))
      }
   }
   fmt.Println("Title case:", string(s)) // Output: Title case: Hello World
}

Output

Original: hello world
Title case: HELLO world

In this example, we create a slice of bytes s with the value "hello world". We then use a for loop to iterate over each byte in the slice. We keep track of whether we are currently in a word using the inWord variable. If the current byte is a space, we set inWord to false. If the current byte is not a space and we are in a word, we convert it to title case using the unicode.ToTitle() function. Finally, we print the original and title case versions of the slice using the fmt.Println() function.

Conclusion

In this article, we learned how to convert a slice of bytes to title case in Golang using the strings.Title() function and a for loop with the unicode.ToTitle() function. The strings.Title() function is the recommended way to convert a slice of bytes to title case as it is more concise and efficient.

Updated on: 20-Apr-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements