summaryrefslogtreecommitdiff
path: root/strings/strings.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-12-29 09:45:38 -0500
committerDave Henderson <dhenderson@gmail.com>2022-12-29 09:45:38 -0500
commitfde6cbeb6868f38d1bb02454a5f6413c91935f5b (patch)
tree49891d1400fbbb05804c662bd1e4a480f4a59ce0 /strings/strings.go
parentdb2957873903b397188d96dd4119f1ed9c2a0ea9 (diff)
Add strings.SkipLines function
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'strings/strings.go')
-rw-r--r--strings/strings.go20
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
+}