Menu Close

Go – bytes.ToTitle() function in go

Here, bytes.ToTitle() function is used to covert slice of bytes to mapped to their title case which means title case is upper case for ASCII.

ToTitle() built-in function of bytes package returns a copy with all the Unicode letters mapped to their title case.

bytes.ToTitle() function prototype:

func ToTitle(s []byte) []byte

Input parameters:
s: slice of bytes

Return:
It returns a copy with all the Unicode letters mapped to their title case.

Explanation:

1)
s := []byte("Hello Techie")

Output: "HELLO TECHIE"

2)
s := []byte("\n\nHello Techie\n\n")

Output: "\n\nHELLO TECHIE\n\n"

3)
s := []byte("&&Hello Techie&&")

Output: "&&HELLO TECHIE&&"

4)
s := []byte("Hello TECHIE")

Output: HELLO TECHIE"

5)
s := []byte("HELLO Techie")

Output: HELLO TECHIE"

Example:

Code 1:

package main

import (
	"bytes"
	"fmt"
)

// main function
func main() {
    
    // trim "$" at right the side
    s := []byte("Hello Techie")
    
    fmt.Printf("%q", bytes.ToTitle(s))
    
    
    // trim "\n" at right the side
    s = []byte("\n\nHello Techie\n\n")
    
    fmt.Printf("\n%q", bytes.ToTitle(s))
    
    
    // trim "" at right the side
    s = []byte("&&Hello Techie&&")
    
    fmt.Printf("\n%q", bytes.ToTitle(s))
    
    
    // trim "&" at right the side
    s = []byte("Hello TECHIE")
    
    fmt.Printf("\n%q", bytes.ToTitle(s))
    
    
    // trim "&" at right the side
    s = []byte("HELLO Techie")
    
    fmt.Printf("\n%q", bytes.ToTitle(s))
    
}

Output:

% go run sample.go

"HELLO TECHIE"
"\n\nHELLO TECHIE\n\n"
"&&HELLO TECHIE&&"
"HELLO TECHIE"
"HELLO TECHIE"

To learn more about golang, Please refer given below link:

References:

https://golang.org/doc/

Posted in bytes, golang, packages

Leave a Reply

Your email address will not be published. Required fields are marked *