From fde6cbeb6868f38d1bb02454a5f6413c91935f5b Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Thu, 29 Dec 2022 09:45:38 -0500 Subject: Add strings.SkipLines function Signed-off-by: Dave Henderson --- strings/strings.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'strings/strings.go') 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 +} -- cgit v1.2.3