Documentation
¶
Overview ¶
Package str provides utility functions for string manipulation.
Index ¶
- func Camelcase(s string) string
- func Capitalize(s string) string
- func Contains(s, substr string) bool
- func Count(s, substr string) int
- func Ellipsis(s string, length int) string
- func EndsWith(s, target string) bool
- func Index(s, substr string) int
- func Join(arr []string, separator string) string
- func KebabCase(s string) string
- func LastIndex(s, substr string) int
- func PascalCase(s string) string
- func Repeat(s string, n int) string
- func Replace(s, old, new string) string
- func RuneLength(str string) int
- func SnakeCase(s string) string
- func Split(s, separator string) []string
- func StartsWith(s, target string) bool
- func ToLower(s string) string
- func ToString[T any](value T) string
- func ToUpper(s string) string
- func Trim(s string, cutset ...string) string
- func TrimEnd(s string, cutset ...string) string
- func TrimStart(s string, cutset ...string) string
- func Words(str string) []string
- func WordsPattern(s, pattern string) []string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Capitalize ¶
Capitalize capitalizes the first character of a string. Example: "fred" -> "Fred"
func Contains ¶
Contains checks if a string contains a substring. Example: Contains("abc", "b") -> true
func Count ¶
Count counts the occurrences of a substring in a string. Example: Count("ababab", "ab") -> 3
func Ellipsis ¶
Ellipsis trims and truncates a string to a specified length in bytes and appends an ellipsis if truncated. It ensures that UTF-8 characters are not split in the middle. Example: Ellipsis("Hello, 世界", 8) -> "Hello, ..."
func EndsWith ¶
EndsWith checks if a string ends with the given target string. Example: EndsWith("abc", "c") -> true
func Index ¶
Index returns the index of the first occurrence of a substring in a string. Returns -1 if the substring is not found. Example: Index("abc", "b") -> 1
func Join ¶
Join joins an array of strings with the given separator. Example: Join(["a", "b", "c"], "-") -> "a-b-c"
func KebabCase ¶
KebabCase converts string to kebab case. Example: "hello world" -> "hello-world" Example: "HelloWorld" -> "hello-world" Example: "HELLO_WORLD" -> "hello-world"
func LastIndex ¶
LastIndex returns the index of the last occurrence of a substring in a string. Returns -1 if the substring is not found. Example: LastIndex("abcabc", "b") -> 4
func PascalCase ¶
PascalCase converts string to pascal case. Example: "hello world" -> "HelloWorld" Example: "hello-world" -> "HelloWorld" Example: "hello_world" -> "HelloWorld"
func Replace ¶
Replace replaces all occurrences of a substring in a string. Example: Replace("Hi Fred", "Fred", "Barney") -> "Hi Barney"
func RuneLength ¶
RuneLength returns the number of runes in a string. Example: RuneLength("Hello, 世界") -> 8
func SnakeCase ¶
SnakeCase converts string to snake case. Example: "hello world" -> "hello_world" Example: "HelloWorld" -> "hello_world" Example: "HELLO-WORLD" -> "hello_world"
func Split ¶
Split splits a string by the given separator. Example: Split("a-b-c", "-") -> ["a", "b", "c"]
func StartsWith ¶
StartsWith checks if a string starts with the given target string. Example: StartsWith("abc", "a") -> true
func ToString ¶
ToString converts any value to its string representation. Example: ToString(123) -> "123"
func Trim ¶
Trim removes leading and trailing whitespace or specified characters from a string. Example: Trim(" abc ") -> "abc"
func TrimEnd ¶
TrimEnd removes trailing whitespace or specified characters from a string. Example: TrimEnd(" abc ") -> " abc"
func TrimStart ¶
TrimStart removes leading whitespace or specified characters from a string. Example: TrimStart(" abc ") -> "abc "
func WordsPattern ¶
WordsPattern splits string into words using a custom pattern. Example: WordsPattern("hello-world_test", `[\-_]+`) -> ["hello", "world", "test"]
Types ¶
This section is empty.