This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Core - String helpersโ
This page lists all string operations available in the core package of lo.
RandomStringโ
Returns a random string of the specified length from the given charset.
str := lo.RandomString(5, lo.LettersCharset)
// e.g., "eIGbt"Prototype:func RandomString(size int, charset []rune) string
Substringโ
Returns a substring starting at the given offset with the specified length. Supports negative offsets; out-of-bounds are clamped.
// Basic usage
result := lo.Substring("hello", 2, 3)
// result: "llo"
// Negative offset - counts from end
result = lo.Substring("hello", -4, 3)
// result: "ell"
// Length longer than string - clamped to available characters
result = lo.Substring("hello", 1, 10)
// result: "ello" (only 4 characters available from position 1)
// Zero length - returns empty string
result = lo.Substring("hello", 1, 0)
// result: ""
// Offset beyond string length - returns empty string
result = lo.Substring("hello", 10, 3)
// result: ""
// With Unicode strings (byte-based)
result = lo.Substring("hรฉllo", 1, 3)
// result: "รฉl" (note: works with bytes, not runes)
// Negative offset with negative values clamped
result = lo.Substring("hello", -10, 3)
// result: "hel" (offset clamped to 0)Prototype:func Substring[T ~string](str T, offset int, length uint) T
ChunkStringโ
Splits a string into chunks of the given size. The last chunk may be shorter. Returns [""] for empty input.
lo.ChunkString("1234567", 2)
// []string{"12", "34", "56", "7"}Prototype:func ChunkString[T ~string](str T, size int) []T
RuneLengthโ
Returns the number of runes (Unicode code points) in a string.
lo.RuneLength("hellรด")
// 5Prototype:func RuneLength(str string) int
PascalCaseโ
Converts a string to PascalCase.
lo.PascalCase("hello_world")
// "HelloWorld"Prototype:func PascalCase(str string) string
CamelCaseโ
Converts a string to camelCase.
lo.CamelCase("hello_world")
// "helloWorld"Prototype:func CamelCase(str string) string
KebabCaseโ
Converts a string to kebab-case.
lo.KebabCase("helloWorld")
// "hello-world"Prototype:func KebabCase(str string) string
SnakeCaseโ
Converts a string to snake_case.
lo.SnakeCase("HelloWorld")
// "hello_world"Prototype:func SnakeCase(str string) string
Wordsโ
Splits a string into a slice of its words, separating letters and digits and removing non-alphanumeric separators.
lo.Words("helloWorld")
// []string{"hello", "world"}Prototype:func Words(str string) []string
Capitalizeโ
Converts the first character to uppercase and the remaining to lowercase.
lo.Capitalize("heLLO")
// "Hello"Prototype:func Capitalize(str string) string
Ellipsisโ
Trims and truncates a string to the specified byte length and appends an ellipsis if truncated.
lo.Ellipsis(" Lorem Ipsum ", 5)
// "Lo..."
str = lo.Ellipsis("Lorem Ipsum", 100)
// "Lorem Ipsum"
str = lo.Ellipsis("Lorem Ipsum", 3)
// "..."Variant:Prototype:func Ellipsis(str string, length int) string