go get -u github.com/ecrmnn/str
Visit https://str.danieleckermann.com for full documentation
- After
- AfterLast
- Append
- At
- Before
- BeforeLast
- Between
- Camel
- Capitalize
- Contains
- ContainsAny
- ContainsAll
- Clone
- Count
- EndsWith
- Exactly
- First
- FirstWord
- Last
- LastWord
- Length
- Lower
- IsEmpty
- IsJson
- IsNotEmpty
- IsUrl
- Kebab
- Pascal
- Prepend
- Repeat
- Remove
- Replace
- Reverse
- Screaming
- Slug
- Snake
- Split
- StartsWith
- String
- Swap
- Title
- TrimAll
- TrimBoth
- TrimLeft
- TrimRight
- Upper
- When
- WhenContains
- WhenContainsAny
- WhenContainsAll
- WhenEndsWith
- WhenEmpty
- WhenExactly
- WhenFunc
- WhenNotEmpty
- WhenNotExactly
- WhenStartsWith
After
returns a new instance of Str containing the substring after the first occurrence of the given substring.
str.New("golang").After("go").String()
// "lang"
str.New("go go go!").After("go").String()
// " go go!"
str.New("ΓΈΓ¦Γ₯").After("Γ¦").String()
// "Γ₯"
str.New("rocket ππ man").After("π").String()
// "π man"
AfterLast
returns a new instance of Str containing the substring after the last occurrence of the given substring.
str.New("golang").AfterLast("go").String()
// "lang"
str.New("go go go!").AfterLast("go").String()
// "!"
str.New("ΓΈΓ¦Γ₯").AfterLast("ΓΈ").String()
// "Γ¦Γ₯"
str.New("rocket ππ man").AfterLast("π").String()
// " man"
Append
appends the given string to the end of the current string and returns a pointer to the current instance of Str.
str.New("golang").Append(" is β₯οΈ").String()
// "golang is β₯οΈ"
str.New("hello").Append("!@#$%^&*()").String()
// "hello!@#$%^&*()"
str.New("").Append("ΓΈΓ¦Γ₯").String()
// "ΓΈΓ¦Γ₯"
str.New("test").Append("π").String()
// "testπ"
At
returns the character at the given index as a string.
str.New("golang").At(2)
// "l"
str.New("hello").At(0)
// "h"
str.New("æøΓ₯").At(1)
// "ΓΈ"
str.New("πxπ").At(2)
// "π"
str.New("").At(0)
// ""
str.New("test").At(10)
// ""
Before
returns a new instance of Str containing the substring before the first occurrence of the given substring.
str.New("golang").Before("go").String()
// ""
str.New("go go go!").Before("go!").String()
// "go go "
str.New("ΓΈΓ¦Γ₯").Before("Γ¦").String()
// "ΓΈ"
str.New("rocket ππ man").Before("π").String()
// "rocket "
str.New("aaa").Before("a").String()
// ""
str.New("aabbcc").Before("bc").String()
// "aab"
BeforeLast
returns a new instance of Str containing the substring before the last occurrence of the given substring.
str.New("golang").BeforeLast("go").String()
// ""
str.New("go go go!").BeforeLast("go").String()
// "go go "
str.New("øæøΓ₯ΓΈΓ₯ΓΈ").BeforeLast("ΓΈ").String()
// "øæøΓ₯ΓΈΓ₯"
str.New("rocket ππ man").BeforeLast("π").String()
// "rocket π"
Between
returns a new instance of Str containing the substring between the first occurrence of the "from" substring and the first occurrence of the "to" substring.
str.New("{{hello}}").Between("{{", "}}").String()
// "hello"
str.New("πβ¨π₯").Between("π", "π₯").String()
// "β¨"
str.New("!#$%").Between("$", "%").String()
// ""
str.New("rocket ππ man").Between("rocket", "man").String()
// " ππ "
str.New("abc").Between("", "c").String()
// "ab"
str.New("abc").Between("a", "").String()
// ""
str.New("abc").Between("", "").String()
// ""
str.New("abc").Between("a", "c").String()
// "b"
str.New("dddabc").Between("a", "c").String()
// "b"
str.New("abcddd").Between("a", "c").String()
// "b"
str.New("dddabcddd").Between("a", "c").String()
// "b"
str.New("hannah").Between("ha", "ah").String()
// "nn"
str.New("[a]ab[b]").Between("[", "]").String()
// "a"
str.New("foofoobar").Between("foo", "bar").String()
// "foo"
str.New("foobarbar").Between("foo", "bar").String()
// ""
Camel
returns a new instance of Str in camelCase.
str.New("string manipulation").Camel().String()
// "stringManipulation"
str.New("").Camel().String()
// ""
str.New("Hello world").Camel().String()
// "helloWorld"
Capitalize
returns a new instance of Str with the first character capitalized.
str.New("hello").Capitalize().String()
// "Hello"
str.New("_ _ _").Capitalize().String()
// "_ _ _"
str.New("hello World").Capitalize().String()
// "Hello World"
Contains
returns true if the current string contains the given substring.
str.New("golang").Contains("lan")
// true
str.New("golang").Contains("")
// true
str.New("golang").Contains("go ")
// false
str.New("lorem ipsum dolor").Contains("um do")
// true
ContainsAny
returns true if the current string contains any of the given substrings.
str.New("golang").
ContainsAny([]string{""}),
// true
str.New("golang").
ContainsAny([]string{}),
// false
str.New("lorem ipsum dolor").
ContainsAny([]string{"lorem", "sit", "amet"}),
// true
str.New("lorem ipsum dolor").
ContainsAny([]string{"sit", "amet"}),
// false
ContainsAll
returns true if the current string contains all of the given substrings.
str.New("golang").
ContainsAll([]string{"lan"}),
// true
str.New("golang").
ContainsAll([]string{}),
// false
str.New("lorem ipsum dolor sit amet").
ContainsAll([]string{"lorem", "sit", "amet"}),
// true
str.New("lorem ipsum dolor sit").
ContainsAll([]string{"sit", "amet"}),
// false
Clone
returns a new instance of Str with the same value as the current instance.
func() bool {
s := str.New("i like go")
if s.Clone().LastWord().Exactly("go") {
s.Append(" β€οΈ")
}
return s.String() == "i like go β€οΈ"
}()
// true
func() bool {
s := str.New("go")
s2 := s.Clone().Append("lang")
// s => go
// s2 => golang
return s.String() == s2.String()
}()
// false
Count
returns the number of occurrences of the given substring in the current string.
str.New("golang").Count("lan")
// 1
str.New("golang").Count("")
// 7
str.New("golang").Count("go ")
// 0
str.New("lorem ipsum dolor").Count("um do")
// 1
str.New("δ½ ε₯½δΈη").Count("ε₯½")
// 1
str.New("δ½ ε₯½δΈη").Count("")
// 5
str.New("δ½ ε₯½δΈη").Count("δ½ ε₯½")
// 1
str.New("δ½ ε₯½δΈη").Count("εθ§")
// 0
EndsWith
returns true if the current string ends with the given substring.
str.New("golang").EndsWith("")
// false
str.New("golang").EndsWith("lang")
// true
str.New(" go ").EndsWith(" ")
// true
str.New("π₯π₯").EndsWith("π₯")
// true
str.New("β¨π₯β¨π₯β¨").EndsWith("π₯")
// false
str.New("go").EndsWith("gox")
// false
str.New("").EndsWith("something")
// false
str.New("abc").EndsWith("")
// false
str.New("abc").EndsWith("abcdef")
// false
str.New("Golang").EndsWith("lang")
// true
str.New("Golang").EndsWith("LANG")
// false
str.New("cafΓ©").EndsWith("fΓ©")
// true
str.New("abcabc").EndsWith("abc")
// true
str.New("abcabc").EndsWith("bc")
// true
Exactly
returns true if the current string is exactly the same as the given string.
str.New("golang").Exactly("golang")
// true
str.New("golang").Exactly("")
// false
str.New("golang").Exactly("go")
// false
str.New("δ½ ε₯½δΈη").Exactly("ε₯½")
// false
str.New("δ½ ε₯½δΈη").Exactly("δ½ ε₯½δΈη")
// true
First
returns s new instance of Str containing the first character of the current string.
str.New("golang").First().String()
// "g"
str.New("hello world").First().String()
// "h"
str.New("Γ¦ ΓΈ Γ₯").First().String()
// "Γ¦"
str.New("π
π").First().String()
// "π
"
str.New("").First().String()
// ""
str.New("γγγ«γ‘γ―").First().String()
// "γ"
str.New("δ½ ε₯½").First().String()
// "δ½ "
FirstWord
returns a new instance of Str containing the first word of the current string.
str.New("golang").FirstWord().String()
// "golang"
str.New("hello world").FirstWord().String()
// "hello"
str.New(" hello it should trim ").FirstWord().String()
// "hello"
str.New("Γ¦ ΓΈ Γ₯").FirstWord().String()
// "Γ¦"
str.New("").FirstWord().String()
// ""
str.New("...hello").FirstWord().String()
// "...hello"
Last
returns s new instance of Str containing the last character of the current string.
str.New("golang").Last().String()
// "g"
str.New("hello world").Last().String()
// "d"
str.New("π₯ Go π€©β¨").Last().String()
// "β¨"
str.New("æøΓ₯").Last().String()
// "Γ₯"
str.New("").Last().String()
// ""
LastWord
returns a new instance of Str containing the last word of the current string.
str.New("golang").LastWord().String()
// "golang"
str.New("hello world").LastWord().String()
// "world"
str.New(" hello it should trim ").LastWord().String()
// "trim"
str.New("Γ¦ ΓΈ Γ₯").LastWord().String()
// "Γ₯"
str.New("").LastWord().String()
// ""
str.New("...hello").LastWord().String()
// "...hello"
Length
returns the length of the current string.
str.New("goπ₯").Length()
// 3
str.New("golang").Length()
// 6
str.New("lorem ipsum dolor").Length()
// 17
str.New("δ½ ε₯½δΈη").Length()
// 4
str.New("δ½ ε₯½η").Length()
// 3
Lower
returns a new instance of Str with all characters in lowercase.
str.New("").Lower().String()
// ""
str.New("β¨").Lower().String()
// "β¨"
str.New("&").Lower().String()
// "&"
str.New("Γ").Lower().String()
// "Γ¦"
str.New("TEST").Lower().String()
// "test"
IsEmpty
returns true if the current string is empty.
str.New("golang").IsEmpty()
// false
str.New("").IsEmpty()
// true
str.New(" ").IsEmpty()
// false
IsJson
returns true if the current string is valid JSON.
str.New(`{"key": "value"}`).IsJson()
// true
str.New(`{"key": "value", "nested": {"inner": 42}}`).IsJson()
// true
str.New("not a valid JSON").IsJson()
// false
str.New("").IsJson()
// false
IsNotEmpty
returns true if the current string is not empty.
str.New("golang").IsNotEmpty()
// true
str.New("").IsNotEmpty()
// false
str.New(" ").IsNotEmpty()
// true
IsUrl
returns true if the current string is a valid URL.
str.New("http://example.com").IsUrl()
// true
str.New("https://subdomain.example.com").IsUrl()
// true
str.New("https://example.com/path").IsUrl()
// true
str.New("https://example.com/path?query=value").IsUrl()
// true
str.New("https://example.com/path#section").IsUrl()
// true
str.New("https://user:[email protected]").IsUrl()
// true
str.New("https://example.com:8080").IsUrl()
// true
str.New("https://my-domain-example.com").IsUrl()
// true
str.New("https://my_domain_example.com").IsUrl()
// false
str.New("https://192.168.0.1").IsUrl()
// true
str.New("ftp://example.com").IsUrl()
// true
str.New("example.com").IsUrl()
// false
str.New("https://").IsUrl()
// false
str.New("https://example.com/").IsUrl()
// true
str.New("https://example!.com").IsUrl()
// false
str.New("https:/example.com").IsUrl()
// false
str.New("https//example.com").IsUrl()
// false
str.New("https://example .com").IsUrl()
// false
str.New("https://localhost:8080").IsUrl()
// true
str.New("https://example&com").IsUrl()
// false
str.New("192.168.0.0").IsUrl()
// false
str.New("http://192.168.0.0").IsUrl()
// true
str.New("https://192.168.0").IsUrl()
// true
str.New("https://π₯.rs").IsUrl()
// true
Kebab
returns a new lowercased instance of Str with all spaces and underscores replaced with hyphens.
str.New("").Kebab().String()
// ""
str.New("go go go").Kebab().String()
// "go-go-go"
str.New("GoLang").Kebab().String()
// "go-lang"
str.New("goLang").Kebab().String()
// "go-lang"
str.New("β¨π₯β¨π₯β¨").Kebab().String()
// "β¨π₯β¨π₯β¨"
str.New("β¨ π₯ β¨ π₯ β¨").Kebab().String()
// "β¨-π₯-β¨-π₯-β¨"
Pascal
returns a new instance of Str in PascalCase.
str.New("").Pascal().String()
// ""
str.New("go go go").Pascal().String()
// "GoGoGo"
str.New("hello world").Pascal().String()
// "HelloWorld"
str.New("Go-lang").Pascal().String()
// "Go-lang"
str.New("goLang").Pascal().String()
// "GoLang"
str.New("β¨π₯β¨π₯β¨").Pascal().String()
// "β¨π₯β¨π₯β¨"
str.New("β¨ π₯ β¨ π₯ β¨").Pascal().String()
// "β¨π₯β¨π₯β¨"
Prepend
prepends the given string to the beginning of the current string and returns a pointer to the current instance of Str.
str.New("β₯οΈ is").Prepend("golang").String()
// "golangβ₯οΈ is"
str.New("!@#$%^&*()").Prepend("hello").String()
// "hello!@#$%^&*()"
str.New("").Prepend("ΓΈΓ¦Γ₯").String()
// "ΓΈΓ¦Γ₯"
str.New("π").Prepend("test").String()
// "testπ"
Repeat
returns a new instance of Str with the current string repeated the given number of times.
str.New("golang").Repeat(3).String()
// "golanggolanggolang"
str.New("golang").Repeat(0).String()
// ""
str.New("lorem ipsum").Repeat(2).String()
// "lorem ipsumlorem ipsum"
str.New("").Repeat(5).String()
// ""
Remove
returns a new instance of Str with all occurrences of the given substring removed.
str.New("i like c++").Remove("c++").String()
// "i like "
str.New("rocket π man").Remove("π").String()
// "rocket man"
str.New("go, go, go!").Remove("go").String()
// ", , !"
str.New("δ½ ε₯½δΈη").Remove("δ½ ε₯½").String()
// "δΈη"
str.New("").Remove("go").String()
// ""
Replace
returns a new instance of Str with all occurrences of the given substring replaced with the given replacement string.
str.New("i like c++").Replace("c++", "go").String()
// "i like go"
str.New("rocket π man").Replace("π", "π").String()
// "rocket π man"
str.New("go, go, go!").Replace("go", "Go").String()
// "Go, Go, Go!"
str.New("δ½ ε₯½δΈη").Replace("δ½ ε₯½", "εθ§").String()
// "εθ§δΈη"
str.New("δ½ ε₯½δΈη").Replace("δ½ ε₯½", "").String()
// "δΈη"
Reverse
returns a new instance of Str with the current string reversed.
str.New("i like c++").Reverse().String()
// "++c ekil i"
str.New("rocket π man").Reverse().String()
// "nam π tekcor"
str.New("go, go, go!").Reverse().String()
// "!og ,og ,og"
str.New("δ½ ε₯½δΈη").Reverse().String()
// "ηδΈε₯½δ½ "
str.New("δ½ ε₯½δΈη").Reverse().String()
// "ηδΈε₯½δ½ "
str.New("go go go").Screaming().String()
// "GO_GO_GO"
str.New("GoLang").Screaming().String()
// "GO_LANG"
str.New("ALREADY_SCREAMING").Screaming().String()
// "ALREADY_SCREAMING"
str.New("").Screaming().String()
// ""
str.New("goLang").Screaming().String()
// "GO_LANG"
str.New("β¨π₯β¨π₯β¨").Screaming().String()
// "β¨π₯β¨π₯β¨"
str.New("β¨ π₯ β¨ π₯ β¨").Screaming().String()
// "β¨_π₯_β¨_π₯_β¨"
Slug
returns a new Str with slugified version of the current string. The separator is used to replace all non-alphanumeric characters.
str.New(" β€οΈ go β€οΈ ").Slug("_").String()
// "go"
str.New(" go β€οΈ ").Slug("_").String()
// "go"
str.New("GoLang").Slug("_").String()
// "golang"
str.New("foo_@_bar-baz!").Slug("_").String()
// "foo_bar_baz"
str.New("foo_@_bar-baz!").Slug("-").String()
// "foo-bar-baz"
str.New("foo_@_bar-baz!").Slug("/").String()
// "foo/bar/baz"
str.New("").Slug("_").String()
// ""
str.New("goLang").Slug("_").String()
// "golang"
str.New("β¨π₯β¨π₯β¨").Slug("_").String()
// ""
str.New("β¨ π₯ β¨ π₯ β¨").Slug("_").String()
// ""
str.New("__hello__world__").Slug("_").String()
// "hello_world"
str.New("__hello__world__").Slug("-").String()
// "hello-world"
Snake
returns a new lowercased instance of Str with all spaces and hyphens replaced with underscores.
str.New("go go go").Snake().String()
// "go_go_go"
str.New("GoLang").Snake().String()
// "go_lang"
str.New("already_snake_cased").Snake().String()
// "already_snake_cased"
str.New("").Snake().String()
// ""
str.New("goLang").Snake().String()
// "go_lang"
str.New("β¨π₯β¨π₯β¨").Snake().String()
// "β¨π₯β¨π₯β¨"
str.New("β¨ π₯ β¨ π₯ β¨").Snake().String()
// "β¨_π₯_β¨_π₯_β¨"
Split
returns a slice of strings containing the substrings of the current string separated by the given separator.
str.New("go").Split("")
// []string{"g", "o"}
str.New("go go go").Split(" ")
// []string{"go", "go", "go"}
str.New("golang").Split(" ")
// []string{"golang"}
str.New("lorem ipsum").Split("_")
// []string{"lorem ipsum"}
str.New("").Split("")
// []string{}
str.New(" go go go").Split("")
// []string{" ", " ", " ", " ", "g", "o", " ", " ", "g", "o", " ", " ", "g", "o"}
str.New("β¨π₯β¨π₯β¨").Split("π₯")
// []string{"β¨", "β¨", "β¨"}
StartsWith
returns true if the current string starts with the given substring.
str.New("golang").StartsWith("")
// false
str.New("golang").StartsWith("go")
// true
str.New(" go ").StartsWith(" ")
// true
str.New("π₯π₯").StartsWith("π₯")
// true
str.New("β¨π₯β¨π₯β¨").StartsWith("π₯")
// false
String
returns the current value as a string.
str.New("go").Capitalize().String()
// "Go"
str.New("").Lower().String()
// ""
Swap
returns a new instance of Str where all occurrences of keys are replaces with their corresponding values.
str.New("I love Rust").
Swap(
map[string]string{
"Rust": "Go",
}).
String()
// "I love Go"
str.New("Rust. Rust! RUST!!").
Swap(
map[string]string{
"Rust": "Go",
}).
String()
// "Go. Go! RUST!!"
str.New("Rust. Rust! RUST!!").
Swap(
map[string]string{
"Rust": "Go",
"RUST": "Go",
}).
String()
// "Go. Go! Go!!"
str.New("I β€οΈ U").
Swap(
map[string]string{
"β€οΈ": "π€¬",
"I": "You",
"U": "Me",
}).
String()
// "You π€¬ Me"
str.New("BLΓ
BΓRSYLTETΓY blΓ₯bΓ¦rsyltetΓΈy").
Swap(
map[string]string{
"Γ": "",
"Γ": "",
"Γ
": "",
"Γ¦": "",
"ΓΈ": "",
"Γ₯": "",
}).
String()
// "BLBRSYLTETY blbrsyltety"
str.New("").
Swap(
map[string]string{
"Γ": "",
"Γ": "",
"Γ
": "",
"Γ¦": "",
"ΓΈ": "",
"Γ₯": "",
}).
String()
// ""
Title
returns a new instance of Str with the first letter of each word capitalized.
str.New("go ΓΈl Γ₯re Γ¦re").Title().String()
// "Go Γl Γ
re Γre"
str.New("go β¨ go").Title().String()
// "Go β¨ Go"
str.New("_ _ _").Title().String()
// "_ _ _"
str.New("").Title().String()
// ""
TrimAll
returns a new instance of Str with all consecutive spaces replaced with a single space.
str.New(" Hello, World ! ").TrimAll().String()
// "Hello, World !"
str.New("Hello,\tWorld ! ").TrimAll().String()
// "Hello, World !"
str.New(" \t\n\t Hello,\tWorld\n!\n\t").TrimAll().String()
// "Hello, World !"
TrimBoth
returns a new instance of Str with all leading and trailing spaces removed.
str.New(" Hello, World ! ").TrimBoth().String()
// "Hello, World !"
str.New("Hello,\tWorld ! ").TrimBoth().String()
// "Hello,\tWorld !"
str.New(" \t\n\t Hello,\tWorld\n!\n\t").TrimBoth().String()
// "Hello,\tWorld\n!"
TrimLeft
returns a new instance of Str with all leading spaces removed.
str.New(" Hello, World ! ").TrimLeft().String()
// "Hello, World ! "
str.New("Hello,\tWorld ! ").TrimLeft().String()
// "Hello,\tWorld ! "
str.New(" \t\n\t Hello,\tWorld\n!\n\t").TrimLeft().String()
// "Hello,\tWorld\n!\n\t"
TrimRight
returns a new instance of Str with all trailing spaces removed.
str.New(" Hello, World ! ").TrimRight().String()
// " Hello, World !"
str.New("Hello,\tWorld ! ").TrimRight().String()
// "Hello,\tWorld !"
str.New(" \t\n\t Hello,\tWorld\n!\n\t").TrimRight().String()
// " \t\n\t Hello,\tWorld\n!"
Upper
returns a new instance of Str with all characters in uppercase.
str.New("").Upper().String()
// ""
str.New("β¨").Upper().String()
// "β¨"
str.New("&").Upper().String()
// "&"
str.New("Γ¦").Upper().String()
// "Γ"
str.New("test").Upper().String()
// "TEST"
When
invokes the given callback if the given condition is true and returns a pointer to the current instance of Str.
str.New("go").
When(true, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "golang"
str.New("go").
When(false, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "go"
str.New("").
When(false, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// ""
WhenContains
invokes the given callback if the current string contains the given value and returns a pointer to the current instance of Str.
str.New("i like go").
WhenContains("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like golang"
str.New("i like go").
WhenContains("gox", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like go"
str.New("").
WhenContains("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// ""
WhenContainsAny
invokes the given callback if the current string contains any of the given values and returns a pointer to the current instance of Str.
str.New("i like go").
WhenContainsAny([]string{"go", "c++"}, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like golang"
str.New("i like go").
WhenContainsAny([]string{"a", "b", "c"}, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like go"
str.New("").
WhenContainsAny([]string{"go", "lang"}, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// ""
WhenContainsAll
invokes the given callback if the current string contains all of the given values and returns a pointer to the current instance of Str.
str.New("i like go not c++").
WhenContainsAll([]string{"go", "c++"}, func(s *str.Str) *str.Str {
return s.Append("!")
}).
String()
// "i like go not c++!"
str.New("i like go").
WhenContainsAll([]string{"a", "b", "c"}, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like go"
str.New("").
WhenContainsAll([]string{"go", "lang"}, func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// ""
WhenEndsWith
invokes the given callback if the current string ends with the given value and returns a pointer to the current instance of Str.
str.New("i like go").
WhenEndsWith("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like golang"
str.New("i like go").
WhenEndsWith("gox", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "i like go"
str.New("").
WhenEndsWith("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// ""
WhenEmpty
invokes the given closure if the current string is empty and returns a pointer to the current instance of Str.
str.New("go").
WhenEmpty(func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "go"
str.New("").
WhenEmpty(func(s *str.Str) *str.Str {
return s.Append("golang")
}).
String()
// "golang"
WhenExactly
invokes the given callback if the current string is exactly the same as the given value and returns a pointer to the current instance of Str.
str.New("go").
WhenExactly("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "golang"
str.New("").
WhenExactly("go", func(s *str.Str) *str.Str {
return s.Append("golang")
}).
String()
// ""
str.New("π").
WhenExactly("π", func(s *str.Str) *str.Str {
return s.Append("β¨")
}).
String()
// "πβ¨"
WhenFunc
invokes the given callback if the given condition is true and returns a pointer to the current instance of Str. The condition function is passed a copy of Str.
str.New("hello world").
WhenFunc(func(s *str.Str) bool {
return s.FirstWord().Exactly("hello")
}, func(s *str.Str) *str.Str {
return s.Append("!")
}).
String()
// "hello world!"
WhenNotEmpty
invokes the given closure if the current string is not empty and returns a pointer to the current instance of Str.
str.New("go").
WhenNotEmpty(func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "golang"
str.New("").
WhenNotEmpty(func(s *str.Str) *str.Str {
return s.Append("golang")
}).
String()
// ""
WhenNotExactly
invokes the given callback if the current string is not exactly the same as the given value and returns a pointer to the current instance of Str.
str.New("go").
WhenNotExactly("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "go"
str.New("").
WhenNotExactly("go", func(s *str.Str) *str.Str {
return s.Append("golang")
}).
String()
// "golang"
str.New("π").
WhenNotExactly("π", func(s *str.Str) *str.Str {
return s.Append("β¨")
}).
String()
// "π"
WhenStartsWith
invokes the given callback if the current string starts with the given value and returns a pointer to the current instance of Str.
str.New("go").
WhenStartsWith("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "golang"
str.New("go").
WhenStartsWith("gox", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// "go"
str.New("").
WhenStartsWith("go", func(s *str.Str) *str.Str {
return s.Append("lang")
}).
String()
// ""