stringy/README.md

422 lines
16 KiB
Markdown
Raw Permalink Normal View History

2020-04-05 16:01:10 +03:00
# Golang String manipulation helper package
![Workflow](https://github.com/gobeam/stringy/actions/workflows/ci.yml/badge.svg) [![Build][Build-Status-Image]][Build-Status-Url] [![Go Report Card](https://goreportcard.com/badge/github.com/gobeam/stringy?branch=master&kill_cache=1)](https://goreportcard.com/report/github.com/gobeam/Stringy) [![GoDoc][godoc-image]][godoc-url]
2020-07-17 11:32:40 +03:00
[![Coverage Status](https://coveralls.io/repos/github/gobeam/stringy/badge.svg)](https://coveralls.io/github/gobeam/stringy)
2020-04-05 16:01:10 +03:00
2020-04-10 10:52:15 +03:00
Convert string to camel case, snake case, kebab case / slugify, custom delimiter, pad string, tease string and many other functionality with help of by Stringy package. You can convert camelcase to snakecase or kebabcase, or snakecase to camelcase and kebabcase and vice versa. This package was inspired from PHP [danielstjules/Stringy](https://github.com/danielstjules/Stringy).
2020-04-05 15:57:30 +03:00
* [Why?](#why)
* [Installation](#installation)
* [Functions](#functions)
* [Running the tests](#running-the-tests)
* [Contributing](#contributing)
* [License](#license)
<table>
<tr>
<td><a href="#betweenstart-end-string-stringmanipulation">Between</a></td>
<td><a href="#boolean-bool">Boolean</a></td>
<td><a href="#camelcaserule-string-string">CamelCase</a></td>
</tr>
<tr>
<td><a href="#containsallcheck-string-bool">ContainsAll</a></td>
<td><a href="#delimiteddelimiter-string-rule-string-stringmanipulation">Delimited</a></td>
<td><a href="#get-string">Get</a></td>
</tr>
<tr>
<td><a href="#kebabcaserule-string-stringmanipulation">KebabCase</a></td>
<td><a href="#lcfirst-string">LcFirst</a></td>
<td><a href="#lines-string">Lines</a></td>
</tr>
<tr>
<td><a href="#padlength-int-with-padtype-string-string">Pad</a></td>
<td><a href="#removespecialcharacter-string">RemoveSpecialCharacter</a></td>
<td><a href="#replacefirstsearch-replace-string-string">ReplaceFirst</a></td>
</tr>
<tr>
<td><a href="#replacelastsearch-replace-string-string">ReplaceLast</a></td>
<td><a href="#reverse-string">Reverse</a></td>
<td><a href="#shuffle-string">Shuffle</a></td>
</tr>
<tr>
<td><a href="#surroundwith-string-string">Surround</a></td>
<td><a href="#snakecaserule-string-stringmanipulation">SnakeCase</a></td>
<td><a href="#teaselength-int-indicator-string-string">Tease</a></td>
</tr>
<tr>
<td><a href="#tolower-string">ToLower</a></td>
<td><a href="#toupper-string">ToUpper</a></td>
<td><a href="#ucfirst-string">UcFirst</a></td>
</tr>
<tr>
<td><a href="#firstlength-int-string">First</a></td>
<td><a href="#lastlength-int-string">Last</a></td>
2021-05-14 20:42:48 +03:00
<td><a href="#prefixstring-string">Prefix</a></td>
</tr>
<tr>
2021-05-14 20:42:48 +03:00
<td><a href="#suffixstring-string">Suffix</a></td>
2023-01-20 08:20:43 +03:00
<td><a href="#acronym-string">Acronym</a></td>
<td><a href="#title-string">Title</a></td>
</tr>
2024-03-16 13:19:07 +03:00
<tr>
<td><a href="#pascalcaserule-string-string">PascalCase</a></td>
<td></td>
<td></td>
</tr>
2020-04-05 15:57:30 +03:00
</table>
## Why?
2020-07-17 12:00:02 +03:00
Golang has very rich strings core package despite some extra helper function are not available and this stringy package is here to fill that void. Plus there are other some packages in golang, that have same functionality but for some extreme cases they fail to provide correct output. This package cross flexibility is it's main advantage. You can convert to camelcase to snakecase or kebabcase or vice versa.
2020-04-05 15:57:30 +03:00
```go
2020-04-05 16:05:30 +03:00
package main
import (
"fmt"
2020-07-17 11:32:40 +03:00
"github.com/gobeam/stringy"
2020-04-05 16:05:30 +03:00
)
func main() {
2020-07-17 11:32:40 +03:00
str := stringy.New("hello__man how-Are you??")
2020-04-05 16:25:29 +03:00
result := str.CamelCase("?", "")
fmt.Println(result) // HelloManHowAreYou
2020-04-05 15:57:30 +03:00
2020-04-05 16:25:29 +03:00
snakeStr := str.SnakeCase("?", "")
fmt.Println(snakeStr.ToLower()) // hello_man_how_are_you
2020-04-05 15:57:30 +03:00
2020-04-05 16:25:29 +03:00
kebabStr := str.KebabCase("?", "")
fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
2020-04-05 16:05:30 +03:00
}
2020-04-05 15:57:30 +03:00
```
## Installation
``` bash
2020-07-17 11:32:40 +03:00
$ go get -u -v github.com/gobeam/stringy
2020-04-05 15:57:30 +03:00
```
or with dep
``` bash
2020-07-17 11:32:40 +03:00
$ dep ensure -add github.com/gobeam/stringy
2020-04-05 15:57:30 +03:00
```
## Functions
2020-04-05 16:05:30 +03:00
#### Between(start, end string) StringManipulation
2020-04-05 15:57:30 +03:00
Between takes two string params start and end which and returns value which is in middle of start and end part of input. You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
```go
2020-07-17 11:32:40 +03:00
strBetween := stringy.New("HelloMyName")
2020-04-05 16:25:29 +03:00
fmt.Println(strBetween.Between("hello", "name").ToUpper()) // MY
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Boolean() bool
2020-04-05 15:57:30 +03:00
Boolean func returns boolean value of string value like on, off, 0, 1, yes, no returns boolean value of string input. You can chain this function on other function which returns implemented StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
boolString := stringy.New("off")
2020-04-05 16:25:29 +03:00
fmt.Println(boolString.Boolean()) // false
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### CamelCase(rule ...string) string
2020-04-05 15:57:30 +03:00
CamelCase is variadic function which takes one Param rule i.e slice of strings and it returns input type string in camel case form and rule helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you dont have to worry about it.
```go
2020-07-17 11:32:40 +03:00
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
2024-03-16 13:19:07 +03:00
fmt.Println(camelCase.CamelCase("?", "", "#", "")) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt
2020-04-05 15:57:30 +03:00
```
look how it omitted ?## from string. If you dont want to omit anything and since it returns plain strings and you cant actually cap all or lower case all camelcase string its not required.
```go
2020-07-17 11:32:40 +03:00
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
2024-03-16 13:19:07 +03:00
fmt.Println(camelCase.CamelCase()) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### ContainsAll(check ...string) bool
2020-04-05 15:57:30 +03:00
ContainsAll is variadic function which takes slice of strings as param and checks if they are present in input and returns boolean value accordingly.
```go
2020-07-17 11:32:40 +03:00
contains := stringy.New("hello mam how are you??")
2020-04-05 16:25:29 +03:00
fmt.Println(contains.ContainsAll("mam", "?")) // true
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Delimited(delimiter string, rule ...string) StringManipulation
2020-04-05 15:57:30 +03:00
Delimited is variadic function that takes two params delimiter and slice of strings named rule. It joins the string by passed delimeter. Rule param helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you dont have to worry about it. If you don't want to omit any character pass empty string.
```go
2020-07-17 11:32:40 +03:00
delimiterString := stringy.New("ThisIsOne___messed up string. Can we Really delimeter-case It?")
2020-04-05 16:25:29 +03:00
fmt.Println(delimiterString.Delimited("?").Get())
2020-04-05 15:57:30 +03:00
```
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
#### First(length int) string
First returns first n characters from provided input. It removes all spaces in string before doing so.
```go
fcn := stringy.New("4111 1111 1111 1111")
first := fcn.First(4)
fmt.Println(first) // 4111
```
2020-04-05 16:05:30 +03:00
#### Get() string
2020-04-05 15:57:30 +03:00
Get simply returns result and can be chained on function which returns StringManipulation interface view above examples
2020-04-05 16:05:30 +03:00
#### KebabCase(rule ...string) StringManipulation
2020-04-05 15:57:30 +03:00
KebabCase/slugify is variadic function that takes one Param slice of strings named rule and it returns passed string in kebab case or slugify form. Rule param helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it. If you don't want to omit any character pass nothing.
```go
2020-07-17 11:32:40 +03:00
str := stringy.New("hello__man how-Are you??")
2020-04-05 16:25:29 +03:00
kebabStr := str.KebabCase("?","")
fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
fmt.Println(kebabStr.Get()) // hello-man-how-Are-you
2020-04-05 15:57:30 +03:00
```
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
#### Last(length int) string
Last returns last n characters from provided input. It removes all spaces in string before doing so.
```go
lcn := stringy.New("4111 1111 1111 1348")
last := lcn.Last(4)
fmt.Println(last) // 1348
```
2020-04-05 16:05:30 +03:00
#### LcFirst() string
2020-04-05 15:57:30 +03:00
LcFirst simply returns result by lower casing first letter of string and it can be chained on function which return StringManipulation interface
```go
2020-07-17 11:32:40 +03:00
contains := stringy.New("Hello roshan")
2020-04-05 16:25:29 +03:00
fmt.Println(contains.LcFirst()) // hello roshan
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Lines() []string
2020-04-05 15:57:30 +03:00
Lines returns slice of strings by removing white space characters
```go
2020-07-17 11:32:40 +03:00
lines := stringy.New("fòô\r\nbàř\nyolo123")
2020-04-05 16:25:29 +03:00
fmt.Println(lines.Lines()) // [fòô bàř yolo123]
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Pad(length int, with, padType string) string
2020-04-05 15:57:30 +03:00
Pad takes three param length i.e total length to be after padding, with i.e what to pad with and pad type which can be ("both" or "left" or "right") it return string after padding upto length by with param and on padType type it can be chained on function which return StringManipulation interface
```go
2020-07-17 11:32:40 +03:00
pad := stringy.New("Roshan")
2020-04-05 16:25:29 +03:00
fmt.Println(pad.Pad(0, "0", "both")) // 00Roshan00
fmt.Println(pad.Pad(0, "0", "left")) // 0000Roshan
fmt.Println(pad.Pad(0, "0", "right")) // Roshan0000
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### RemoveSpecialCharacter() string
2020-04-05 15:57:30 +03:00
RemoveSpecialCharacter removes all special characters and returns the string nit can be chained on function which return StringManipulation interface
```go
2020-07-17 11:32:40 +03:00
cleanString := stringy.New("special@#remove%%%%")
2020-04-05 16:25:29 +03:00
fmt.Println(cleanString.RemoveSpecialCharacter()) // specialremove
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### ReplaceFirst(search, replace string) string
2020-04-05 15:57:30 +03:00
ReplaceFirst takes two param search and replace. It returns string by searching search sub string and replacing it with replace substring on first occurrence it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
replaceFirst := stringy.New("Hello My name is Roshan and his name is Alis.")
2020-04-05 16:25:29 +03:00
fmt.Println(replaceFirst.ReplaceFirst("name", "nombre")) // Hello My nombre is Roshan and his name is Alis.
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### ReplaceLast(search, replace string) string
2020-04-05 15:57:30 +03:00
ReplaceLast takes two param search and replace it return string by searching search sub string and replacing it with replace substring on last occurrence it can be chained on function which return StringManipulation interface
```go
2020-07-17 11:32:40 +03:00
replaceLast := stringy.New("Hello My name is Roshan and his name is Alis.")
2020-04-05 16:25:29 +03:00
fmt.Println(replaceLast.ReplaceLast("name", "nombre")) // Hello My name is Roshan and his nombre is Alis.
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Reverse() string
2020-04-05 15:57:30 +03:00
Reverse function reverses the passed strings it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
reverse := stringy.New("This is only test")
2020-04-05 16:25:29 +03:00
fmt.Println(reverse.Reverse()) // tset ylno si sihT
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Shuffle() string
2020-04-05 15:57:30 +03:00
Shuffle shuffles the given string randomly it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
shuffleString := stringy.New("roshan")
2020-04-05 16:25:29 +03:00
fmt.Println(shuffleString.Shuffle()) // nhasro
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### Surround(with string) string
2020-04-05 15:57:30 +03:00
Surround takes one param with which is used to surround user input and it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
surroundStr := stringy.New("__")
2020-04-05 16:25:29 +03:00
fmt.Println(surroundStr.Surround("-")) // -__-
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### SnakeCase(rule ...string) StringManipulation
2020-04-05 15:57:30 +03:00
SnakeCase is variadic function that takes one Param slice of strings named rule and it returns passed string in snake case form. Rule param helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it. If you don't want to omit any character pass nothing.
```go
2020-07-17 11:32:40 +03:00
snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
2020-04-05 16:25:29 +03:00
fmt.Println(snakeCase.SnakeCase("?", "").Get()) // This_Is_One_messed_up_string_Can_we_Really_Snake_Case_It
fmt.Println(snakeCase.SnakeCase("?", "").ToUpper()) // THIS_IS_ONE_MESSED_UP_STRING_CAN_WE_REALLY_SNAKE_CASE_IT
2020-04-05 15:57:30 +03:00
```
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
2020-04-05 16:05:30 +03:00
#### Tease(length int, indicator string) string
2020-04-05 15:57:30 +03:00
Tease takes two params length and indicator and it shortens given string on passed length and adds indicator on end it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
teaseString := stringy.New("Hello My name is Roshan. I am full stack developer")
2020-04-05 16:25:29 +03:00
fmt.Println(teaseString.Tease(20, "...")) // Hello My name is Ros...
2020-04-05 15:57:30 +03:00
```
#### Title() string
Title returns string with first letter of each word in uppercase it can be chained on function which return StringManipulation interface.
```go
title := stringy.New("hello roshan")
fmt.Println(title.Title()) // Hello Roshan
```
2020-04-05 15:57:30 +03:00
2020-04-05 16:05:30 +03:00
#### ToLower() string
2020-04-05 15:57:30 +03:00
ToLower makes all string of user input to lowercase and it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
2020-04-05 16:25:29 +03:00
fmt.Println(snakeCase.SnakeCase("?", "").ToLower()) // this_is_one_messed_up_string_can_we_really_snake_case_it
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### ToUpper() string
2020-04-05 15:57:30 +03:00
ToUpper makes all string of user input to uppercase and it can be chained on function which return StringManipulation interface.
```go
2020-07-17 11:32:40 +03:00
snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
2020-04-05 16:25:29 +03:00
fmt.Println(snakeCase.SnakeCase("?", "").ToUpper()) // THIS_IS_ONE_MESSED_UP_STRING_CAN_WE_REALLY_SNAKE_CASE_IT
2020-04-05 15:57:30 +03:00
```
2020-04-05 16:05:30 +03:00
#### UcFirst() string
2020-04-05 15:57:30 +03:00
UcFirst simply returns result by upper casing first letter of string and it can be chained on function which return StringManipulation interface.
2020-04-05 15:57:30 +03:00
```go
2020-07-17 11:32:40 +03:00
contains := stringy.New("hello roshan")
2020-04-05 16:25:29 +03:00
fmt.Println(contains.UcFirst()) // Hello roshan
2020-04-05 15:57:30 +03:00
```
#### Prefix(string) string
Prefix makes sure string has been prefixed with a given string and avoids adding it again if it has.
```go
ufo := stringy.New("known flying object")
fmt.Println(ufo.Prefix("un")) // unknown flying object
```
#### Suffix(string) string
Suffix makes sure string has been suffixed with a given string and avoids adding it again if it has.
```go
pun := stringy.New("this really is a cliff")
fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger
```
2023-01-20 08:20:43 +03:00
#### Acronym() string
Acronym func returns acronym of input string. You can chain ToUpper() which with make result all upercase or ToLower() which will make result all lower case or Get which will return result as it is
```go
acronym := stringy.New("Laugh Out Loud")
fmt.Println(acronym.Acronym().ToLower()) // lol
```
2024-03-16 13:19:07 +03:00
#### PascalCase(rule ...string) string
PascalCase is variadic function which takes one Param rule i.e slice of strings and it returns input type string in pascal case form and rule helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it.
```go
pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really pascal-case It ?##")
fmt.Println(pascalCase.PascalCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyPascalCaseIt
```
look how it omitted ?## from string. If you dont want to omit anything and since it returns plain strings and you cant actually cap all or lower case all camelcase string it's not required.
```go
pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
fmt.Println(pascalCase.PascalCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
```
2023-01-20 08:20:43 +03:00
2020-04-05 15:57:30 +03:00
## Running the tests
``` bash
$ go test
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate. - see `CONTRIBUTING.md` for details.
2020-04-05 15:57:30 +03:00
## License
Released under the MIT License - see `LICENSE.txt` for details.
2020-04-21 11:58:41 +03:00
[Build-Status-Url]: https://travis-ci.com/gobeam/stringy
[Build-Status-Image]: https://travis-ci.com/gobeam/stringy.svg?branch=master
2020-07-17 11:32:40 +03:00
[godoc-url]: https://pkg.go.dev/github.com/gobeam/stringy?tab=doc
[godoc-image]: https://godoc.org/github.com/gobeam/stringy?status.svg
2020-04-05 15:57:30 +03:00