Files
lo/docs/data/core-substring.md
T
2025-10-06 17:15:49 +02:00

1.3 KiB

name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory playUrl variantHelpers similarHelpers position signatures
Substring substring string.go#L105 core string https://go.dev/play/p/TQlxQi82Lu1
core#string#substring
core#string#chunkstring
core#string#runelength
core#string#ellipsis
core#string#words
core#slice#replace
core#slice#replaceall
10
func Substring[T ~string](str T, offset int, length uint) T

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)