Skip to content

How to Change Case (Upper Case, Lower Case, Sentence Case, Capitalize Each Word / Title Case) of Text / String in Golang ?

Change Case is a function that can change the case of a string. It can be used in Golang.

LowerCase

ToLower is a function that changes the case of a string to lowercase. ToLower converts the given input string to lowercase. It removes leading and trailing white spaces.

Parameters:

  • input: The string to be converted to lowercase.

Returns:

  • The lowercase representation of the input string.
func ToLower(input string) string {
	// Trim leading and trailing white spaces and convert the string to lowercase
	return strings.TrimSpace(strings.ToLower(input))
}

UpperCase

ToUpper is a function that changes the case of a string to uppercase. ToUpper converts the given input string to uppercase. It removes leading and trailing white spaces.

Parameters:

  • input: The string to be converted to uppercase.

Returns:

  • The uppercase representation of the input string.
func ToUpper(input string) string {
	// Trim leading and trailing white spaces and convert the string to uppercase
	return strings.TrimSpace(strings.ToUpper(input))
}

SentenceCase

ToSentenceCase is a function that changes the case of a string to sentence case. ToSentenceCase changes the case of a string to sentence case.

Parameters:

  • input: The string to be converted to sentence case.

Returns:

  • The sentence case representation of the input string.
func ToSentenceCase(input string) string {
	// Convert the input string to lowercase
	input = ToLower(input)

	// If the input string is empty, return an empty string
	if len(input) <= 0 {
		return ""
	}

	// Split the input string into words
	words := strings.Split(input, " ")

	// Convert the first word to title case
	words[0] = ToTitle(words[0])

	// Join the words back together with spaces
	return strings.Join(words, " ")
}

Capitalize Each Word / TitleCase

ToTitle is a function that changes the case of a string to title case. ToTitle converts the first character of each word in the input string to uppercase. It ignores non-letter characters.

Parameters:

  • input: The string to be converted to title case.

Returns:

  • The title case representation of the input string.
func ToTitle(input string) string {
	var (
		output []rune // stores the converted string
		isWord = true // flag to determine if we are currently inside a word
	)

	// iterate over each character in the input string
	for _, val := range input {
		// if we are inside a word, check if the character is a letter
		// if it is, convert it to uppercase and set the isWord flag to false
		if isWord && unicode.IsLetter(val) {
			output = append(output, unicode.ToUpper(val))
			isWord = false
		} else if !unicode.IsLetter(val) { // if the character is not a letter, set the isWord flag to true
			isWord = true
			output = append(output, val)
		} else { // if we are not inside a word, add the character to the output
			output = append(output, val)
		}
	}

	return strings.TrimSpace(string(output)) // return the converted string
}

Experience the magic of coding unfold as you execute this code!

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func ToLower(input string) string {
	return strings.TrimSpace(strings.ToLower(input))
}

func ToUpper(input string) string {
	return strings.TrimSpace(strings.ToUpper(input))
}

func ToSentenceCase(input string) string {
	input = ToLower(input)
	if len(input) <= 0 {
		return ""
	}

	words := strings.Split(input, " ")
	words[0] = ToTitle(words[0])
	return strings.Join(words, " ")
}

func ToTitle(input string) string {
	var (
		output []rune 
		isWord = true 
	)

	for _, val := range input {
		if isWord && unicode.IsLetter(val) {
			output = append(output, unicode.ToUpper(val))
			isWord = false
		} else if !unicode.IsLetter(val) {
			isWord = true
			output = append(output, val)
		} else {
			output = append(output, val)
		}
	}

	return strings.TrimSpace(string(output)) 
}

func main() {
	fmt.Println(ToLower("Lorem ipsum dolor sit amet."))        // output; lorem ipsum dolor sit amet.
	fmt.Println(ToUpper("Lorem ipsum dolor sit amet."))        // output; LOREM IPSUM DOLOR SIT AMET.
	fmt.Println(ToSentenceCase("LOREM IPSUM DOLOR SIT AMET.")) // output; Lorem ipsum dolor sit amet.
	fmt.Println(ToTitle("Lorem ipsum dolor sit amet."))        // output; Lorem Ipsum Dolor Sit Amet.
}

enjoy your code!