How to make the first letter of a string lowercase in Go Language?

How to make the first letter of a string lowercase, but not change the case of any of the other letters?

For example: 

"Go language is awesome" → "go language is awesome"
"Hello world" → "hello world"


Solution 1:

package main

import (
"unicode"
"unicode/utf8"
)

// Main function
func main() {

str := "Go language is awesome"
println(firstToLower(str))
}

func firstToLower(s string) string {
if len(s) > 0 {
r, size := utf8.DecodeRuneInString(s)
if r != utf8.RuneError || size > 1 {
lower := unicode.ToLower(r)
if lower != r {
s = string(lower) + s[size:]
}
}
}
return s
}

Output:




Solution 2: Easy

package main

import "strings"

// Main function
func main() {

str := "Go language is awesome"
str = strings.ToLower(string(str[0])) + str[1:]
println(str)
}

Output:




Solution 3: Normal

package main

import (
"unicode"
)

// Main function
func main() {

str := "Go language is awesome"
runes := []rune(str)
if len(runes) > 0 {
runes[0] = unicode.ToLower(runes[0])
}
println(string(runes))
}

Output:


Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024