diff options
| author | Jeff Carr <[email protected]> | 2025-09-05 01:52:54 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-09-05 01:52:54 -0500 |
| commit | 4efd88947cc7abd2e42d7d4e6e9fb1ce81def8e0 (patch) | |
| tree | 7f514228fb892f151cb57d2c5dfe32390393c702 /splitNewLines.go | |
| parent | e621f228badee0f27d04055cfe4381264bdf2abf (diff) | |
Diffstat (limited to 'splitNewLines.go')
| -rw-r--r-- | splitNewLines.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/splitNewLines.go b/splitNewLines.go new file mode 100644 index 0000000..7ee7442 --- /dev/null +++ b/splitNewLines.go @@ -0,0 +1,17 @@ +package shell + +import "regexp" + +// splits strings. should work all the time +// A string with mixed line endings, including old Mac style (\r) +func SplitNewLines(input string) []string { + // This regex matches a carriage return and optional newline, OR just a newline. + // This covers \r\n, \n, and \r. + re := regexp.MustCompile(`\r\n?|\n|\r`) + + // The -1 means there is no limit to the number of splits. + lines := re.Split(input, -1) + + // Output: ["line one" "line two" "line three" "line four"] + return lines +} |
