goStr
is a Go package that provides functions for string manipulation, you are free to use them in your own applications if you find them convenient.
To use goStr
in your Go project, you can install it using the go get
command:
go get github.com/bhaktibuana/go-str
After
method returns everything after the given value in a string. The entire string will be returned if the value does not exist within the string.
/*
* @param input string
* @param substr string
* @returns string
*/
func After(input, substr string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.After("The quick brown fox jumps over the lazy dog", "brown")
// " fox jumps over the lazy dog"
AfterLast
method returns everything after the last occurrence of the given value in a string. The entire string will be returned if the value does not exist within the string.
/*
* @param input string
* @param substr string
* @returns string
*/
func AfterLast(input, substr string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.AfterLast("https://www.example.com/user/profile", "/")
// "profile"
APA
method converts the given string to title case following the APA guidelines. (Supported language: English)
/*
* @param input string
* @returns string
*/
func APA(input string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.APA("createing a project")
// "Creating a Project"
Ascii
method will attempt to transliterate the string into an ASCII value.
/*
* @param input string
* @returns string
*/
func Ascii(input string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Ascii("û")
// "u"
Before
method returns everything before the given value in a string.
/*
* @param input string
* @param substr string
* @returns string
*/
func Before(input, substr string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Before("The quick brown fox jumps over the lazy dog", "fox")
// "The quick brown "
BeforeLast
method returns everything before the last occurrence of the given value in a string.
/*
* @param input string
* @param substr string
* @returns string
*/
func BeforeLast(input, substr string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.BeforeLast("www.example.com/user/profile", "/")
// "www.example.com/user"
Camel
method converts the given string to camelCase.
/*
* @param input string
* @returns string
*/
func Camel(input string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Camel("foo_bar")
// "fooBar"
Currency
method will convert an amount of number or string into selected country code currency format.
/*
* @param amount interface{} (int | float64 | string)
* @param code CurrencyCode (string)
* @param options ...bool (useDecimal true, dotSeparator false, useSpacer true)
* @returns string
*/
func Currency(amount interface{}, code CurrencyCode, options ...bool) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Currency(77.5, CURRENCY_USD)
// "$ 77.50"
goStr.Currency(77.5, CURRENCY_USD, true, false, false)
// "$77.50"
goStr.Currency(12500, CURRENCY_IDR, true, true)
// "Rp 12.500,00"
Headline
method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized.
/*
* @param input string
* @returns string
*/
func Headline(input string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Headline("HelloWorld")
// "Hello World"
goStr.Headline("foo_bar")
// "Foo Bar"
Limit
method truncates the given string to the specified length.
/*
* @param input string
* @param length int
* @param appendStr string
* @returns string
*/
func Limit(input string, length int, appendStr ...string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Limit("The quick brown fox jumps over the lazy dog", 20)
// The quick brown fox...
You may pass a third argument to the method to change the string that will be appended to the end of the truncated string
import "github.com/bhaktibuana/go-str"
goStr.Limit("The quick brown fox jumps over the lazy dog", 20, " (...)")
// The quick brown fox (...)
Snake
method converts the given string to snake_case.
/*
* @param input string
* @returns string
*/
func Snake(input string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Snake("myName-is john")
// "my_name_is_john"
Ucfirst
method returns the given string with the first character capitalized.
/*
* @param input string
* @returns string
*/
func Ucfirst(input string) string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Ucfirst("foo bar")
// "Foo bar"
Ucsplit
method splits the given string into a collection by uppercase characters.
/*
* @param input string
* @returns []string
*/
func Ucsplit(input string) []string
- usage
import "github.com/bhaktibuana/go-str"
goStr.Ucsplit("HelloWorld")
// ["Hello", "World"]