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"PascalCaseWithLanguageuses a locale-aware title caser. This matters for languages such as Turkish, where the uppercase ofiisİ(dotted I), notI.lo.PascalCaseWithLanguage("istanbul_city", language.Turkish)// "İstanbulCity"Variant:Prototypes:func PascalCase(str string) stringfunc PascalCaseWithLanguage(str string, tag language.Tag) stringCamelCase
Converts a string to camelCase.
lo.CamelCase("hello_world")// "helloWorld"CamelCaseWithLanguageuses locale-aware casing. The first word is fully lowercased, subsequent words are title-cased, both according to the given language tag.lo.CamelCaseWithLanguage("ISTANBUL_CITY", language.Turkish)// "ıstanbulCıty" (capital I → ı, title I → İ in Turkish)Variant:Prototypes:func CamelCase(str string) stringfunc CamelCaseWithLanguage(str string, tag language.Tag) stringKebabCase
Converts a string to kebab-case.
lo.KebabCase("helloWorld")// "hello-world"KebabCaseWithLanguageuses a locale-aware lowercase caser instead ofstrings.ToLower. This matters for languages such as Turkish, where capitalIlowercases toı(dotless i, U+0131), noti.lo.KebabCaseWithLanguage("ISTANBUL_CITY", language.Turkish)// "ıstanbul-cıty"lo.KebabCaseWithLanguage("ISTANBUL_CITY", language.English)// "istanbul-city"Variant:Prototypes:func KebabCase(str string) stringfunc KebabCaseWithLanguage(str string, tag language.Tag) stringSnakeCase
Converts a string to snake_case.
lo.SnakeCase("HelloWorld")// "hello_world"SnakeCaseWithLanguageuses a locale-aware lowercase caser instead ofstrings.ToLower. This matters for languages such as Turkish, where capitalIlowercases toı(dotless i, U+0131), noti.lo.SnakeCaseWithLanguage("ISTANBUL_CITY", language.Turkish)// "ıstanbul_cıty"lo.SnakeCaseWithLanguage("ISTANBUL_CITY", language.English)// "istanbul_city"Variant:Prototypes:func SnakeCase(str string) stringfunc SnakeCaseWithLanguage(str string, tag language.Tag) 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"CapitalizeWithLanguageuses a locale-aware title caser. This matters for languages such as Turkish, where the uppercase ofiisİ(dotted I), notI.lo.CapitalizeWithLanguage("istanbul", language.Turkish)// "İstanbul"lo.CapitalizeWithLanguage("istanbul", language.English)// "Istanbul"Variant:Prototypes:func Capitalize(str string) stringfunc CapitalizeWithLanguage(str string, tag language.Tag) 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