Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions strafter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package strutil

import "strings"

func StrAfter(str string, search string) string {
pos := strings.Index(str, search)
return StrAfterIndex(str, search, pos)
}

func StrAfterLast(str string, search string) string {
pos := strings.LastIndex(str, search)
return StrAfterIndex(str, search, pos)
}

func StrAfterIndex(str string, search string, pos int) string {
if pos == -1 {
return ""
}
adjustedPos := pos + len(search)
if adjustedPos >= len(str) {
return ""
}
return strings.Trim(str[adjustedPos:len(str)], " ")
}
34 changes: 34 additions & 0 deletions strafter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package strutil

import "testing"

func TestStrAfter(t *testing.T) {
tests := []struct {
input string
search string
expected string
}{
{"Return every word after this word", "word", "after this word"},
{"it's not contained", "nothing", ""},
}

for i, test := range tests {
output := StrAfter(test.input, test.search)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}

func TestStrAfterLast(t *testing.T) {
tests := []struct {
input string
search string
expected string
}{
{"Return every word after the word last occurance", "word", "last occurance"},
}

for i, test := range tests {
output := StrAfterLast(test.input, test.search)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}
19 changes: 19 additions & 0 deletions strbefore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package strutil

import "strings"

func StrBefore(str string, search string) string {
pos := strings.Index(str, search)
if pos == -1 {
return ""
}
return strings.Trim(str[0:pos], " ")
}

func StrBeforeLast(str string, search string) string {
pos := strings.LastIndex(str, search)
if pos == -1 {
return ""
}
return strings.Trim(str[0:pos], " ")
}
33 changes: 33 additions & 0 deletions strbefore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package strutil

import "testing"

func TestStrBefore(t *testing.T) {
tests := []struct {
input string
search string
expected string
}{
{"before this", "this", "before"},
}

for i, test := range tests {
output := StrBefore(test.input, test.search)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}

func TestStrBeforeLast(t *testing.T) {
tests := []struct {
input string
search string
expected string
}{
{"before this is this", "this", "before this is"},
}

for i, test := range tests {
output := StrBeforeLast(test.input, test.search)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}
19 changes: 19 additions & 0 deletions strbetween.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package strutil

import "strings"

func StrBetween(str string, a string, b string) string {
posFirst := strings.Index(str, a)
if posFirst == -1 {
return ""
}
posLast := strings.Index(str, b)
if posLast == -1 {
return ""
}
posFirstAdjusted := posFirst + len(a)
if posFirstAdjusted >= posLast {
return ""
}
return strings.Trim(str[posFirstAdjusted:posLast], " ")
}
19 changes: 19 additions & 0 deletions strbetween_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package strutil

import "testing"

func TestStrBetween(t *testing.T) {
tests := []struct {
input string
a string
b string
expected string
}{
{"between this and that", "this", "that", "and"},
}

for i, test := range tests {
output := StrBetween(test.input, test.a, test.b)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}