File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments