diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2022-12-29 09:45:38 -0500 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2022-12-29 09:45:38 -0500 |
| commit | fde6cbeb6868f38d1bb02454a5f6413c91935f5b (patch) | |
| tree | 49891d1400fbbb05804c662bd1e4a480f4a59ce0 /strings/strings.go | |
| parent | db2957873903b397188d96dd4119f1ed9c2a0ea9 (diff) | |
Add strings.SkipLines function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'strings/strings.go')
| -rw-r--r-- | strings/strings.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/strings/strings.go b/strings/strings.go index 269e760f..073375e7 100644 --- a/strings/strings.go +++ b/strings/strings.go @@ -2,6 +2,7 @@ package strings import ( + "fmt" "regexp" "sort" "strings" @@ -125,3 +126,22 @@ func WordWrap(in string, opts WordWrapOpts) string { opts = wwDefaults(opts) return goutils.WrapCustom(in, int(opts.Width), opts.LBSeq, false) } + +// SkipLines - skip the given number of lines (ending with \n) from the string. +// If skip is greater than the number of lines in the string, an empty string is +// returned. +func SkipLines(skip int, in string) (string, error) { + if skip < 0 { + return "", fmt.Errorf("skip must be >= 0") + } + if skip == 0 { + return in, nil + } + + lines := strings.SplitN(in, "\n", skip+1) + if skip >= len(lines) { + return "", nil + } + + return lines[skip], nil +} |
