Skip to content

Commit 9118c40

Browse files
committed
CamelCase resolved
1 parent 860084e commit 9118c40

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/easy/CamelCase.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
// CamelCase converts the input string to camel case format.
10+
func CamelCase(str string) string {
11+
words := splitWords(str)
12+
var camel strings.Builder
13+
for i, word := range words {
14+
if i == 0 {
15+
camel.WriteString(word)
16+
} else {
17+
camel.WriteString(strings.ToUpper(word[:1]) + word[1:])
18+
}
19+
}
20+
return camel.String()
21+
}
22+
23+
// splitWords is a helper function which splits the input string into words.
24+
func splitWords(str string) []string {
25+
str = strings.ToLower(str)
26+
re := regexp.MustCompile("[^a-z]+")
27+
words := re.Split(str, -1)
28+
var result []string
29+
for _, word := range words {
30+
if word != "" {
31+
result = append(result, word)
32+
}
33+
}
34+
return result
35+
}
36+
37+
func main() {
38+
result1 := CamelCase("_good_looking_blues_")
39+
fmt.Println(result1)
40+
41+
result2 := CamelCase("-=last-night-on-earth=-")
42+
fmt.Println(result2)
43+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy