Home > database >  How to capitalize the first letter of a string
How to capitalize the first letter of a string

Time:12-03

this is my first question!

So here's my question.

I have a string like this

var sentence string
string = "the biggest ocean is the Pacific ocean"

I want to be able to make the t in the to be capitalized so the string is

"The biggest ocean is the Pacific ocean"

Thanks for any replies!

CodePudding user response:

Get the first rune, title case that rune and reassemble the string:

sentence := "the biggest ocean is the Pacific ocean"
r, i := utf8.DecodeRuneInString(sentence)
sentence = string(unicode.ToTitle(r))   sentence[i:]
fmt.Println(sentence)

CodePudding user response:

I have simple solution for you.

Its a fork I have of someones project on Github

https://github.com/CleanMachine1/capitalise

To use it just run in a terminal:

go mod init MODULENAME
go get github.com/cleanmachine1/capitalise

then in your code you can use


package main

import ("github.com/cleanmachine1/capitalise")

func main(){
 sentence = capitalise.First(sentence)
}
  •  Tags:  
  • go
  • Related