Created
July 23, 2019 13:30
-
-
Save betandr/cd65015e46736bf3499e4010c7abf625 to your computer and use it in GitHub Desktop.
Expand a CamelCase string to a Title string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"unicode" | |
) | |
// expandTitle turns a string like NewYork into New York by finding a capital | |
// letter and inserting a space. | |
func camelToTitle(name string) string { | |
for i, c := range name { | |
if unicode.IsUpper(c) && i > 0 { | |
name = name[:i] + " " + name[i:] | |
} | |
} | |
return name | |
} | |
func main() { | |
fmt.Println(expandTitle("NewYork")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment