Skip to main content

String - Core helpers

This page lists all string operations available in the core package of lo.

Help improve this documentation

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!

  • 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
  • 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 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 (rune-aware)
    result = lo.Substring("héllo", 1, 3)
    // result: "él"

    // 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
  • 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
  • Returns the number of runes (Unicode code points) in a string.

    lo.RuneLength("hellô")
    // 5
    Prototype:
    func RuneLength(str string) int
  • Converts a string to PascalCase.

    lo.PascalCase("hello_world")
    // "HelloWorld"

    PascalCaseWithLanguage uses a locale-aware title caser. This matters for languages such as Turkish, where the uppercase of i is İ (dotted I), not I.

    lo.PascalCaseWithLanguage("istanbul_city", language.Turkish)
    // "İstanbulCity"
    Prototypes:
    func PascalCase(str string) string
    func PascalCaseWithLanguage(str string, tag language.Tag) string
  • Converts a string to camelCase.

    lo.CamelCase("hello_world")
    // "helloWorld"

    CamelCaseWithLanguage uses 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)
    Prototypes:
    func CamelCase(str string) string
    func CamelCaseWithLanguage(str string, tag language.Tag) string
  • Converts a string to kebab-case.

    lo.KebabCase("helloWorld")
    // "hello-world"

    KebabCaseWithLanguage uses a locale-aware lowercase caser instead of strings.ToLower. This matters for languages such as Turkish, where capital I lowercases to ı (dotless i, U+0131), not i.

    lo.KebabCaseWithLanguage("ISTANBUL_CITY", language.Turkish)
    // "ıstanbul-cıty"

    lo.KebabCaseWithLanguage("ISTANBUL_CITY", language.English)
    // "istanbul-city"
    Prototypes:
    func KebabCase(str string) string
    func KebabCaseWithLanguage(str string, tag language.Tag) string
  • Converts a string to snake_case.

    lo.SnakeCase("HelloWorld")
    // "hello_world"

    SnakeCaseWithLanguage uses a locale-aware lowercase caser instead of strings.ToLower. This matters for languages such as Turkish, where capital I lowercases to ı (dotless i, U+0131), not i.

    lo.SnakeCaseWithLanguage("ISTANBUL_CITY", language.Turkish)
    // "ıstanbul_cıty"

    lo.SnakeCaseWithLanguage("ISTANBUL_CITY", language.English)
    // "istanbul_city"
    Prototypes:
    func SnakeCase(str string) string
    func SnakeCaseWithLanguage(str string, tag language.Tag) string
  • 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
  • Converts the first character to uppercase and the remaining to lowercase.

    lo.Capitalize("heLLO")
    // "Hello"

    CapitalizeWithLanguage uses a locale-aware title caser. This matters for languages such as Turkish, where the uppercase of i is İ (dotted I), not I.

    lo.CapitalizeWithLanguage("istanbul", language.Turkish)
    // "İstanbul"

    lo.CapitalizeWithLanguage("istanbul", language.English)
    // "Istanbul"
    Prototypes:
    func Capitalize(str string) string
    func CapitalizeWithLanguage(str string, tag language.Tag) string
  • 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