String - Core helpers
This page lists all string operations available in the core package of lo.
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!
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) stringSubstringโ
Returns a substring starting at the given offset with the specified length. Supports negative offsets; out-of-bounds are clamped. Operates on Unicode runes (characters) and is optimized for zero allocations.
// Basic usageresult := lo.Substring("hello", 2, 3)// result: "llo"// Negative offset - counts from endresult = lo.Substring("hello", -4, 3)// result: "ell"// Length longer than string - clamped to available charactersresult = lo.Substring("hello", 1, 10)// result: "ello" (only 4 characters available from position 1)// Zero length - returns empty stringresult = lo.Substring("hello", 1, 0)// result: ""// Offset beyond string length - returns empty stringresult = lo.Substring("hello", 10, 3)// result: ""// With Unicode strings (rune-aware)result = lo.Substring("hรฉllo", 1, 3)// result: "รฉl"// Negative offset with negative values clampedresult = lo.Substring("hello", -10, 3)// result: "hel" (offset clamped to 0)Prototype:func Substring[T ~string](str T, offset int, length uint) TChunkStringโ
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) []TRuneLengthโ
Returns the number of runes (Unicode code points) in a string.
lo.RuneLength("hellรด")// 5Prototype:func RuneLength(str string) intPascalCaseโ
Converts a string to PascalCase.
lo.PascalCase("hello_world")// "HelloWorld"Prototype:func PascalCase(str string) stringCamelCaseโ
Converts a string to camelCase.
lo.CamelCase("hello_world")// "helloWorld"Prototype:func CamelCase(str string) stringKebabCaseโ
Converts a string to kebab-case.
lo.KebabCase("helloWorld")// "hello-world"Prototype:func KebabCase(str string) stringSnakeCaseโ
Converts a string to snake_case.
lo.SnakeCase("HelloWorld")// "hello_world"Prototype:func SnakeCase(str string) stringWordsโ
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) []stringCapitalizeโ
Converts the first character to uppercase and the remaining to lowercase.
lo.Capitalize("heLLO")// "Hello"Prototype:func Capitalize(str string) stringEllipsisโ
Trims and truncates a string to the specified length in runes (Unicode code points) and appends an ellipsis if truncated. Multi-byte characters such as emoji or CJK ideographs are never split in the middle.
lo.Ellipsis(" Lorem Ipsum ", 5)// "Lo..."str = lo.Ellipsis("Lorem Ipsum", 100)// "Lorem Ipsum"str = lo.Ellipsis("Lorem Ipsum", 3)// "..."str = lo.Ellipsis("hello ไธ็! ไฝ ๅฅฝ", 8)// "hello..."str = lo.Ellipsis("๐ ๐ถ๐ฑ๐", 4)// "๐ ๐ถ๐ฑ๐"Prototype:func Ellipsis(str string, length int) string