Skip to content

Commit 6b076a4

Browse files
committed
refactor Camelcase
1 parent 9118c40 commit 6b076a4

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/easy/CamelCase.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,23 @@ package main
22

33
import (
44
"fmt"
5-
"regexp"
65
"strings"
76
)
87

98
// CamelCase converts the input string to camel case format.
109
func CamelCase(str string) string {
11-
words := splitWords(str)
12-
var camel strings.Builder
10+
words := strings.FieldsFunc(strings.ToLower(str), func(r rune) bool {
11+
return !('a' <= r && r <= 'z')
12+
})
13+
var camel string
1314
for i, word := range words {
1415
if i == 0 {
15-
camel.WriteString(word)
16+
camel += word
1617
} else {
17-
camel.WriteString(strings.ToUpper(word[:1]) + word[1:])
18+
camel += strings.Title(word)
1819
}
1920
}
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
21+
return camel
3522
}
3623

3724
func main() {

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