mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-12 00:47:26 +02:00
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package tools
|
|
|
|
import "strings"
|
|
|
|
// RemoveFirstLines removes the first n lines. Returns "" if n >= total lines.
|
|
func RemoveFirstLines(input string, n int) string {
|
|
lines := strings.Split(input, "\n")
|
|
if n >= len(lines) {
|
|
return ""
|
|
}
|
|
return strings.Join(lines[n:], "\n")
|
|
}
|
|
|
|
// RemoveLastLines removes the last n lines. Returns "" if n >= total lines.
|
|
func RemoveLastLines(input string, n int) string {
|
|
lines := strings.Split(input, "\n")
|
|
if n >= len(lines) {
|
|
return ""
|
|
}
|
|
return strings.Join(lines[:len(lines)-n], "\n")
|
|
}
|
|
|
|
// CropBefore removes everything before the first occurrence of y (inclusive of y).
|
|
// Returns input unchanged if y is not found.
|
|
func CropBefore(input string, y string) string {
|
|
idx := strings.Index(input, y)
|
|
if idx == -1 {
|
|
return input
|
|
}
|
|
return input[idx:]
|
|
}
|
|
|
|
// CropAfter removes everything after the last occurrence of y (inclusive of y).
|
|
// Returns input unchanged if y is not found.
|
|
func CropAfter(input string, y string) string {
|
|
idx := strings.LastIndex(input, y)
|
|
if idx == -1 {
|
|
return input
|
|
}
|
|
return input[:idx+len(y)]
|
|
}
|
|
|
|
// CropBeforeExclude removes everything before and including the first occurrence of y.
|
|
// Returns input unchanged if y is not found.
|
|
func CropBeforeExclude(input string, y string) string {
|
|
idx := strings.Index(input, y)
|
|
if idx == -1 {
|
|
return input
|
|
}
|
|
return input[idx+len(y):]
|
|
}
|
|
|
|
// CropAfterExclude removes everything from the last occurrence of y onwards.
|
|
// Returns input unchanged if y is not found.
|
|
func CropAfterExclude(input string, y string) string {
|
|
idx := strings.LastIndex(input, y)
|
|
if idx == -1 {
|
|
return input
|
|
}
|
|
return input[:idx]
|
|
}
|