Skip to content

How to Change Case (Upper Case, Lower Case, Sentence Case, Capitalize Each Word / Title Case, Capitalize Words target) 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 _, r := range input {
		// Check if the character is a letter
		if unicode.IsLetter(r) {
			// If the character is the first character of a word
			if isWord {
				// Convert the character to uppercase
				output = append(output, unicode.ToUpper(r))
				// Set the flag to indicate that we are no longer at the start of a word
				isWord = false
			} else {
				// Convert the character to lowercase
				output = append(output, unicode.ToLower(r))
			}
		} else {
			// Add non-letter characters to the output string
			output = append(output, r)
			// Set the flag to indicate that we are now at the start of a word
			isWord = true
		}
	}

	return strings.TrimSpace(string(output)) // Return the converted string, trimmed of any leading or trailing whitespace
}

Capitalize Words target

CapitalizeWordsTarget capitalizes the words that are specified in the targets slice from the input string. It returns the capitalized string.

The function works by creating a map of the target words and then iterating over the words in the input string. If a word is found in the map, it is capitalized. The function then returns the capitalized string with the words joined by spaces.

func CapitalizeWordsTarget(input string, targets []string) string {
	// create a map of the target words
	targetMap := make(map[string]bool)
	for _, t := range targets {
		targetMap[strings.ToLower(t)] = true
	}

	// split the input string into words
	words := strings.Fields(input)

	// iterate over the words and capitalize if necessary
	for i, w := range words {
		if targetMap[strings.ToLower(w)] {
			words[i] = strings.ToUpper(w)
		}
	}

	// return the words and the ones that are capitalized are the targets then combined with spaces again
	return strings.Join(words, " ")
}

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 _, r := range input {
		if unicode.IsLetter(r) {
			if isWord {
				output = append(output, unicode.ToUpper(r))
				isWord = false
			} else {
				output = append(output, unicode.ToLower(r))
			}
		} else {
			output = append(output, r)
			isWord = true
		}
	}

	return strings.TrimSpace(string(output))
}

func CapitalizeWordsTarget(input string, targets []string) string {
	targetMap := make(map[string]bool)
	for _, t := range targets {
		targetMap[strings.ToLower(t)] = true
	}

	words := strings.Fields(input)

	for i, w := range words {
		if targetMap[strings.ToLower(w)] {
			words[i] = strings.ToUpper(w)
		}
	}

	return strings.Join(words, " ")
}

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.
	fmt.Println(CapitalizeWordsTarget("Lorem ipsum dolor sit amet.", []string{"sit", "ipsum"})) // output; Lorem IPSUM Dolor SIT Amet.
}

enjoy your code!