diff --git a/README_zh-CN.md b/README_zh-CN.md index 2aa9198..566b81d 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -1999,7 +1999,7 @@ import "github.com/duke-git/lancet/v2/strutil" - **BeforeLast** : 返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#BeforeLast)] [[play](https://go.dev/play/p/pJfXXAoG_Te)] -- **CamelCase** : 将字符串转换为 CamelCase 驼峰式字符串, 非字母和数字会被忽略。 +- **CamelCase** : 将字符串转换为camelCase驼峰式字符串, 非字母和数字会被忽略。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#CamelCase)] [[play](https://go.dev/play/p/9eXP3tn2tUy)] - **Capitalize** : 将字符串的第一个字符转换为大写。 diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index b5d370b..8252bfe 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -232,7 +232,7 @@ func main() { ### CamelCase -

将字符串转换为驼峰式字符串, 非字母和数字会被忽略。

+

将字符串转换为驼峰式字符串,首字母小写, 非字母和数字会被忽略。

函数签名: diff --git a/strutil/string.go b/strutil/string.go index 46b5357..6767de2 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -34,6 +34,23 @@ func CamelCase(s string) string { return builder.String() } +// UpperCamelCase coverts string to UpperCamelCase string. Non letters and numbers will be ignored. +// Play: https://go.dev/play/p/todo +func UpperCamelCase(s string) string { + var builder strings.Builder + + strs := splitIntoStrings(s, false) + for _, str := range strs { + // if i == 0 { + // builder.WriteString(strings.ToLower(str)) + // } else { + builder.WriteString(Capitalize(str)) + // } + } + + return builder.String() +} + // Capitalize converts the first character of a string to upper case and the remaining to lower case. // Play: https://go.dev/play/p/2OAjgbmAqHZ func Capitalize(s string) string { diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index a8bf402..e15cf6d 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -94,6 +94,21 @@ func ExampleCamelCase() { // foo11Bar } +func ExampleUpperCamelCase() { + strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"} + + for _, v := range strings { + s := UpperCamelCase(v) + fmt.Println(s) + } + // Output: + // + // Foobar + // FooBarBaz + // Foo + // Foo11Bar +} + func ExampleCapitalize() { strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"} diff --git a/strutil/string_test.go b/strutil/string_test.go index b47443b..cca0889 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -30,6 +30,30 @@ func TestCamelCase(t *testing.T) { } } +func TestUpperCamelCase(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestUpperCamelCase") + + tests := []struct { + input string + expected string + }{ + {"", ""}, + {"foobar", "Foobar"}, + {"&FOO:BAR$BAZ", "FooBarBaz"}, + {"fooBar", "FooBar"}, + {"FOObar", "FoObar"}, + {"$foo%", "Foo"}, + {" $#$Foo 22 bar ", "Foo22Bar"}, + {"Foo-#1😄$_%^&*(1bar", "Foo11Bar"}, + } + + for _, tt := range tests { + assert.Equal(tt.expected, UpperCamelCase(tt.input)) + } +} + func TestCapitalize(t *testing.T) { t.Parallel()