mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
doc: update document and add playgound example for strutil package
This commit is contained in:
@@ -9,9 +9,8 @@ import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// CamelCase covert string to camelCase string.
|
||||
// non letters and numbers will be ignored
|
||||
// eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"
|
||||
// CamelCase coverts string to camelCase string. Non letters and numbers will be ignored.
|
||||
// Play: https://go.dev/play/p/9eXP3tn2tUy
|
||||
func CamelCase(s string) string {
|
||||
var builder strings.Builder
|
||||
|
||||
@@ -28,6 +27,7 @@ func CamelCase(s string) 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 {
|
||||
result := make([]rune, len(s))
|
||||
for i, v := range s {
|
||||
@@ -42,6 +42,7 @@ func Capitalize(s string) string {
|
||||
}
|
||||
|
||||
// UpperFirst converts the first character of string to upper case.
|
||||
// Play: https://go.dev/play/p/sBbBxRbs8MM
|
||||
func UpperFirst(s string) string {
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
@@ -54,6 +55,7 @@ func UpperFirst(s string) string {
|
||||
}
|
||||
|
||||
// LowerFirst converts the first character of string to lower case.
|
||||
// Play: https://go.dev/play/p/CbzAyZmtJwL
|
||||
func LowerFirst(s string) string {
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
@@ -67,6 +69,7 @@ func LowerFirst(s string) string {
|
||||
|
||||
// PadEnd pads string on the right side if it's shorter than size.
|
||||
// Padding characters are truncated if they exceed size.
|
||||
// Play: https://go.dev/play/p/9xP8rN0vz--
|
||||
func PadEnd(source string, size int, padStr string) string {
|
||||
len1 := len(source)
|
||||
len2 := len(padStr)
|
||||
@@ -86,6 +89,7 @@ func PadEnd(source string, size int, padStr string) string {
|
||||
|
||||
// PadStart pads string on the left side if it's shorter than size.
|
||||
// Padding characters are truncated if they exceed size.
|
||||
// Play: https://go.dev/play/p/xpTfzArDfvT
|
||||
func PadStart(source string, size int, padStr string) string {
|
||||
len1 := len(source)
|
||||
len2 := len(padStr)
|
||||
@@ -103,39 +107,36 @@ func PadStart(source string, size int, padStr string) string {
|
||||
return fill[0:size-len1] + source
|
||||
}
|
||||
|
||||
// KebabCase covert string to kebab-case
|
||||
// non letters and numbers will be ignored
|
||||
// eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"
|
||||
// KebabCase coverts string to kebab-case, non letters and numbers will be ignored.
|
||||
// Play: https://go.dev/play/p/dcZM9Oahw-Y
|
||||
func KebabCase(s string) string {
|
||||
result := splitIntoStrings(s, false)
|
||||
return strings.Join(result, "-")
|
||||
}
|
||||
|
||||
// UpperKebabCase covert string to upper KEBAB-CASE
|
||||
// non letters and numbers will be ignored
|
||||
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR"
|
||||
// UpperKebabCase coverts string to upper KEBAB-CASE, non letters and numbers will be ignored
|
||||
// Play: https://go.dev/play/p/zDyKNneyQXk
|
||||
func UpperKebabCase(s string) string {
|
||||
result := splitIntoStrings(s, true)
|
||||
return strings.Join(result, "-")
|
||||
}
|
||||
|
||||
// SnakeCase covert string to snake_case
|
||||
// non letters and numbers will be ignored
|
||||
// eg. "Foo-#1😄$_%^&*(1bar" => "foo_1_1_bar"
|
||||
// SnakeCase coverts string to snake_case, non letters and numbers will be ignored
|
||||
// Play: https://go.dev/play/p/tgzQG11qBuN
|
||||
func SnakeCase(s string) string {
|
||||
result := splitIntoStrings(s, false)
|
||||
return strings.Join(result, "_")
|
||||
}
|
||||
|
||||
// UpperSnakeCase covert string to upper SNAKE_CASE
|
||||
// non letters and numbers will be ignored
|
||||
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR"
|
||||
// UpperSnakeCase coverts string to upper SNAKE_CASE, non letters and numbers will be ignored
|
||||
// Play: https://go.dev/play/p/4COPHpnLx38
|
||||
func UpperSnakeCase(s string) string {
|
||||
result := splitIntoStrings(s, true)
|
||||
return strings.Join(result, "_")
|
||||
}
|
||||
|
||||
// Before create substring in source string before position when char first appear
|
||||
// Before returns the substring of the source string up to the first occurrence of the specified string.
|
||||
// Play: https://go.dev/play/p/JAWTZDS4F5w
|
||||
func Before(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
return s
|
||||
@@ -144,7 +145,8 @@ func Before(s, char string) string {
|
||||
return s[0:i]
|
||||
}
|
||||
|
||||
// BeforeLast create substring in source string before position when char last appear
|
||||
// BeforeLast returns the substring of the source string up to the last occurrence of the specified string.
|
||||
// Play: https://go.dev/play/p/pJfXXAoG_Te
|
||||
func BeforeLast(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
return s
|
||||
@@ -163,7 +165,8 @@ func After(s, char string) string {
|
||||
return s[i+len(char):]
|
||||
}
|
||||
|
||||
// AfterLast create substring in source string after position when char last appear
|
||||
// AfterLast returns the substring after the last occurrence of a specified string in the source string.
|
||||
// Play: https://go.dev/play/p/1TegARrb8Yn
|
||||
func AfterLast(s, char string) string {
|
||||
if s == "" || char == "" {
|
||||
return s
|
||||
@@ -173,6 +176,7 @@ func AfterLast(s, char string) string {
|
||||
}
|
||||
|
||||
// IsString check if the value data type is string or not.
|
||||
// Play: https://go.dev/play/p/IOgq7oF9ERm
|
||||
func IsString(v any) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -185,7 +189,8 @@ func IsString(v any) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse return string whose char order is reversed to the given string
|
||||
// Reverse returns string whose char order is reversed to the given string.
|
||||
// Play: https://go.dev/play/p/adfwalJiecD
|
||||
func Reverse(s string) string {
|
||||
r := []rune(s)
|
||||
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
|
||||
@@ -194,7 +199,8 @@ func Reverse(s string) string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
// Wrap a string with another string.
|
||||
// Wrap a string with given string.
|
||||
// Play: https://go.dev/play/p/KoZOlZDDt9y
|
||||
func Wrap(str string, wrapWith string) string {
|
||||
if str == "" || wrapWith == "" {
|
||||
return str
|
||||
@@ -207,7 +213,8 @@ func Wrap(str string, wrapWith string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// Unwrap a given string from anther string. will change str value
|
||||
// Unwrap a given string from anther string. will change source string.
|
||||
// Play: https://go.dev/play/p/Ec2q4BzCpG-
|
||||
func Unwrap(str string, wrapToken string) string {
|
||||
if str == "" || wrapToken == "" {
|
||||
return str
|
||||
@@ -225,7 +232,8 @@ func Unwrap(str string, wrapToken string) string {
|
||||
return str
|
||||
}
|
||||
|
||||
// SplitEx split a given string whether the result contains empty string
|
||||
// SplitEx split a given string which can control the result slice contains empty string or not.
|
||||
// Play: https://go.dev/play/p/Us-ySSbWh-3
|
||||
func SplitEx(s, sep string, removeEmptyString bool) []string {
|
||||
if sep == "" {
|
||||
return []string{}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package strutil
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ExampleAfter() {
|
||||
result1 := After("foo", "")
|
||||
@@ -191,6 +193,7 @@ func ExamplePadEnd() {
|
||||
result4 := PadEnd("foo", 4, "bar")
|
||||
result5 := PadEnd("foo", 5, "bar")
|
||||
result6 := PadEnd("foo", 6, "bar")
|
||||
result7 := PadEnd("foo", 7, "bar")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
@@ -198,6 +201,7 @@ func ExamplePadEnd() {
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
fmt.Println(result6)
|
||||
fmt.Println(result7)
|
||||
// Output:
|
||||
// foo
|
||||
// foo
|
||||
@@ -205,6 +209,7 @@ func ExamplePadEnd() {
|
||||
// foob
|
||||
// fooba
|
||||
// foobar
|
||||
// foobarb
|
||||
}
|
||||
|
||||
func ExamplePadStart() {
|
||||
@@ -214,6 +219,7 @@ func ExamplePadStart() {
|
||||
result4 := PadStart("foo", 4, "bar")
|
||||
result5 := PadStart("foo", 5, "bar")
|
||||
result6 := PadStart("foo", 6, "bar")
|
||||
result7 := PadStart("foo", 7, "bar")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
@@ -221,6 +227,7 @@ func ExamplePadStart() {
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
fmt.Println(result6)
|
||||
fmt.Println(result7)
|
||||
// Output:
|
||||
// foo
|
||||
// foo
|
||||
@@ -228,6 +235,7 @@ func ExamplePadStart() {
|
||||
// bfoo
|
||||
// bafoo
|
||||
// barfoo
|
||||
// barbfoo
|
||||
}
|
||||
|
||||
func ExampleReverse() {
|
||||
|
||||
Reference in New Issue
Block a user