mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
1.3 KiB
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 |
|
|
10 |
|
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)