I have a string like this
var sentence string = "the biggest ocean is the Pacific ocean"
I want to be able to capitalize the first letter t
in the input string, so that the string becomes
"The biggest ocean is the Pacific ocean"
How to do that in Go?
I have tried using strings.Title
and strings.ToTitle
however they don't do what I want.
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)
Assuming that your input string is valid UTF-8 , this thread ( Golang - ToUpper() on a single byte? ) is close enough, though not quite a perfect duplicate. We can build on that to come to an acceptable solution using unicode.ToUpper
on the first rune of the string.
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
s := string(r)
Or with a "clever" one-liner:
s := string(append([]rune{unicode.ToUpper(r[0])}, r[1:]...))
Unlike strings, rune slices are not immutable, so you can replace the first rune with ToUpper
, which will take care of non-ASCII and/or multi-byte code points that do have upper cases (eg Russian) and leave alone those that don't (eg Asian scripts)
NOTE: there is a difference between UPPER case and TITLE case, which is simply explained here . In short, digraph characters like DŽ will have different title case (Dž, only first grapheme capitalized) and upper cases (DŽ, both graphemes capitalized). If you actually need titlecase, use unicode.ToTitle
.
NOTE 2: converting to/from string
to []rune
involves copying, because you get a mutable slice from an immutable string. Do profile your application if you expect to use it in performance-sensitive code.
Playground: https://go.dev/play/p/HpCBM7cRflZ
Here's a couple of ways of doing this
The first alters the bit pattern in the first byte of the sentence
The second calls a upper casing function on a string made of the first character of the sentence
Both make a new newsentence string as strings are immutable, they can't be altered
package main
import (
"fmt"
"strings"
)
func methodb() {
var sentence = "the biggest ocean is the Pacific ocean"
var b = []byte(sentence)
b[0] = b[0] & 223
var newsentence = string(b)
fmt.Println(newsentence)
}
func methoda() {
var sentence = "the biggest ocean is the Pacific ocean"
var newsentence = fmt.Sprintf("%s%s", strings.ToUpper(string(sentence[0])), sentence[1:])
fmt.Println(newsentence)
}
func main() {
methoda()
methodb()
}
The simplest way to achieve the desired result is to use strings.ToUpper()
function. Refer
var input string = "the biggest ocean is the Pacific ocean"
res := strings.ToUpper(input[:1]) + input[1:]
fmt.Println(res)
OR
You can try it on goplayground
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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.