| 1 |
# xstrings |
| 2 |
|
| 3 |
[](https://github.com/huandu/xstrings/actions) |
| 4 |
[](https://pkg.go.dev/github.com/huandu/xstrings) |
| 5 |
[](https://goreportcard.com/report/github.com/huandu/xstrings) |
| 6 |
[](https://coveralls.io/github/huandu/xstrings?branch=master) |
| 7 |
|
| 8 |
Go package [xstrings](https://godoc.org/github.com/huandu/xstrings) is a collection of string functions, which are widely used in other languages but absent in Go package [strings](http://golang.org/pkg/strings). |
| 9 |
|
| 10 |
All functions are well tested and carefully tuned for performance. |
| 11 |
|
| 12 |
## Propose a new function |
| 13 |
|
| 14 |
Please review [contributing guideline](CONTRIBUTING.md) and [create new issue](https://github.com/huandu/xstrings/issues) to state why it should be included. |
| 15 |
|
| 16 |
## Install |
| 17 |
|
| 18 |
Use `go get` to install this library. |
| 19 |
|
| 20 |
go get github.com/huandu/xstrings |
| 21 |
|
| 22 |
## API document |
| 23 |
|
| 24 |
See [GoDoc](https://godoc.org/github.com/huandu/xstrings) for full document. |
| 25 |
|
| 26 |
## Function list |
| 27 |
|
| 28 |
Go functions have a unique naming style. One, who has experience in other language but new in Go, may have difficulties to find out right string function to use. |
| 29 |
|
| 30 |
Here is a list of functions in [strings](http://golang.org/pkg/strings) and [xstrings](https://godoc.org/github.com/huandu/xstrings) with enough extra information about how to map these functions to their friends in other languages. Hope this list could be helpful for fresh gophers. |
| 31 |
|
| 32 |
### Package `xstrings` functions |
| 33 |
|
| 34 |
_Keep this table sorted by Function in ascending order._ |
| 35 |
|
| 36 |
| Function | Friends | # | |
| 37 |
| --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | --------------------------------------------------- | |
| 38 |
| [Center](https://godoc.org/github.com/huandu/xstrings#Center) | `str.center` in Python; `String#center` in Ruby | [#30](https://github.com/huandu/xstrings/issues/30) | |
| 39 |
| [Count](https://godoc.org/github.com/huandu/xstrings#Count) | `String#count` in Ruby | [#16](https://github.com/huandu/xstrings/issues/16) | |
| 40 |
| [Delete](https://godoc.org/github.com/huandu/xstrings#Delete) | `String#delete` in Ruby | [#17](https://github.com/huandu/xstrings/issues/17) | |
| 41 |
| [ExpandTabs](https://godoc.org/github.com/huandu/xstrings#ExpandTabs) | `str.expandtabs` in Python | [#27](https://github.com/huandu/xstrings/issues/27) | |
| 42 |
| [FirstRuneToLower](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) | `lcfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) | |
| 43 |
| [FirstRuneToUpper](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper) | `String#capitalize` in Ruby; `ucfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) | |
| 44 |
| [Insert](https://godoc.org/github.com/huandu/xstrings#Insert) | `String#insert` in Ruby | [#18](https://github.com/huandu/xstrings/issues/18) | |
| 45 |
| [LastPartition](https://godoc.org/github.com/huandu/xstrings#LastPartition) | `str.rpartition` in Python; `String#rpartition` in Ruby | [#19](https://github.com/huandu/xstrings/issues/19) | |
| 46 |
| [LeftJustify](https://godoc.org/github.com/huandu/xstrings#LeftJustify) | `str.ljust` in Python; `String#ljust` in Ruby | [#28](https://github.com/huandu/xstrings/issues/28) | |
| 47 |
| [Len](https://godoc.org/github.com/huandu/xstrings#Len) | `mb_strlen` in PHP | [#23](https://github.com/huandu/xstrings/issues/23) | |
| 48 |
| [Partition](https://godoc.org/github.com/huandu/xstrings#Partition) | `str.partition` in Python; `String#partition` in Ruby | [#10](https://github.com/huandu/xstrings/issues/10) | |
| 49 |
| [Reverse](https://godoc.org/github.com/huandu/xstrings#Reverse) | `String#reverse` in Ruby; `strrev` in PHP; `reverse` in Perl | [#7](https://github.com/huandu/xstrings/issues/7) | |
| 50 |
| [RightJustify](https://godoc.org/github.com/huandu/xstrings#RightJustify) | `str.rjust` in Python; `String#rjust` in Ruby | [#29](https://github.com/huandu/xstrings/issues/29) | |
| 51 |
| [RuneWidth](https://godoc.org/github.com/huandu/xstrings#RuneWidth) | - | [#27](https://github.com/huandu/xstrings/issues/27) | |
| 52 |
| [Scrub](https://godoc.org/github.com/huandu/xstrings#Scrub) | `String#scrub` in Ruby | [#20](https://github.com/huandu/xstrings/issues/20) | |
| 53 |
| [Shuffle](https://godoc.org/github.com/huandu/xstrings#Shuffle) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) | |
| 54 |
| [ShuffleSource](https://godoc.org/github.com/huandu/xstrings#ShuffleSource) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) | |
| 55 |
| [Slice](https://godoc.org/github.com/huandu/xstrings#Slice) | `mb_substr` in PHP | [#9](https://github.com/huandu/xstrings/issues/9) | |
| 56 |
| [Squeeze](https://godoc.org/github.com/huandu/xstrings#Squeeze) | `String#squeeze` in Ruby | [#11](https://github.com/huandu/xstrings/issues/11) | |
| 57 |
| [Successor](https://godoc.org/github.com/huandu/xstrings#Successor) | `String#succ` or `String#next` in Ruby | [#22](https://github.com/huandu/xstrings/issues/22) | |
| 58 |
| [SwapCase](https://godoc.org/github.com/huandu/xstrings#SwapCase) | `str.swapcase` in Python; `String#swapcase` in Ruby | [#12](https://github.com/huandu/xstrings/issues/12) | |
| 59 |
| [ToCamelCase](https://godoc.org/github.com/huandu/xstrings#ToCamelCase) | `String#camelize` in RoR | [#1](https://github.com/huandu/xstrings/issues/1) | |
| 60 |
| [ToKebab](https://godoc.org/github.com/huandu/xstrings#ToKebabCase) | - | [#41](https://github.com/huandu/xstrings/issues/41) | |
| 61 |
| [ToPascalCase](https://godoc.org/github.com/huandu/xstrings#ToPascalCase) | - | [#1](https://github.com/huandu/xstrings/issues/1) | |
| 62 |
| [ToSnakeCase](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) | `String#underscore` in RoR | [#1](https://github.com/huandu/xstrings/issues/1) | |
| 63 |
| [Translate](https://godoc.org/github.com/huandu/xstrings#Translate) | `str.translate` in Python; `String#tr` in Ruby; `strtr` in PHP; `tr///` in Perl | [#21](https://github.com/huandu/xstrings/issues/21) | |
| 64 |
| [Width](https://godoc.org/github.com/huandu/xstrings#Width) | `mb_strwidth` in PHP | [#26](https://github.com/huandu/xstrings/issues/26) | |
| 65 |
| [WordCount](https://godoc.org/github.com/huandu/xstrings#WordCount) | `str_word_count` in PHP | [#14](https://github.com/huandu/xstrings/issues/14) | |
| 66 |
| [WordSplit](https://godoc.org/github.com/huandu/xstrings#WordSplit) | - | [#14](https://github.com/huandu/xstrings/issues/14) | |
| 67 |
|
| 68 |
### Package `strings` functions |
| 69 |
|
| 70 |
_Keep this table sorted by Function in ascending order._ |
| 71 |
|
| 72 |
| Function | Friends | |
| 73 |
| --------------------------------------------------------------- | ----------------------------------------------------------------------------------- | |
| 74 |
| [Contains](http://golang.org/pkg/strings/#Contains) | `String#include?` in Ruby | |
| 75 |
| [ContainsAny](http://golang.org/pkg/strings/#ContainsAny) | - | |
| 76 |
| [ContainsRune](http://golang.org/pkg/strings/#ContainsRune) | - | |
| 77 |
| [Count](http://golang.org/pkg/strings/#Count) | `str.count` in Python; `substr_count` in PHP | |
| 78 |
| [EqualFold](http://golang.org/pkg/strings/#EqualFold) | `stricmp` in PHP; `String#casecmp` in Ruby | |
| 79 |
| [Fields](http://golang.org/pkg/strings/#Fields) | `str.split` in Python; `split` in Perl; `String#split` in Ruby | |
| 80 |
| [FieldsFunc](http://golang.org/pkg/strings/#FieldsFunc) | - | |
| 81 |
| [HasPrefix](http://golang.org/pkg/strings/#HasPrefix) | `str.startswith` in Python; `String#start_with?` in Ruby | |
| 82 |
| [HasSuffix](http://golang.org/pkg/strings/#HasSuffix) | `str.endswith` in Python; `String#end_with?` in Ruby | |
| 83 |
| [Index](http://golang.org/pkg/strings/#Index) | `str.index` in Python; `String#index` in Ruby; `strpos` in PHP; `index` in Perl | |
| 84 |
| [IndexAny](http://golang.org/pkg/strings/#IndexAny) | - | |
| 85 |
| [IndexByte](http://golang.org/pkg/strings/#IndexByte) | - | |
| 86 |
| [IndexFunc](http://golang.org/pkg/strings/#IndexFunc) | - | |
| 87 |
| [IndexRune](http://golang.org/pkg/strings/#IndexRune) | - | |
| 88 |
| [Join](http://golang.org/pkg/strings/#Join) | `str.join` in Python; `Array#join` in Ruby; `implode` in PHP; `join` in Perl | |
| 89 |
| [LastIndex](http://golang.org/pkg/strings/#LastIndex) | `str.rindex` in Python; `String#rindex`; `strrpos` in PHP; `rindex` in Perl | |
| 90 |
| [LastIndexAny](http://golang.org/pkg/strings/#LastIndexAny) | - | |
| 91 |
| [LastIndexFunc](http://golang.org/pkg/strings/#LastIndexFunc) | - | |
| 92 |
| [Map](http://golang.org/pkg/strings/#Map) | `String#each_codepoint` in Ruby | |
| 93 |
| [Repeat](http://golang.org/pkg/strings/#Repeat) | operator `*` in Python and Ruby; `str_repeat` in PHP | |
| 94 |
| [Replace](http://golang.org/pkg/strings/#Replace) | `str.replace` in Python; `String#sub` in Ruby; `str_replace` in PHP | |
| 95 |
| [Split](http://golang.org/pkg/strings/#Split) | `str.split` in Python; `String#split` in Ruby; `explode` in PHP; `split` in Perl | |
| 96 |
| [SplitAfter](http://golang.org/pkg/strings/#SplitAfter) | - | |
| 97 |
| [SplitAfterN](http://golang.org/pkg/strings/#SplitAfterN) | - | |
| 98 |
| [SplitN](http://golang.org/pkg/strings/#SplitN) | `str.split` in Python; `String#split` in Ruby; `explode` in PHP; `split` in Perl | |
| 99 |
| [Title](http://golang.org/pkg/strings/#Title) | `str.title` in Python | |
| 100 |
| [ToLower](http://golang.org/pkg/strings/#ToLower) | `str.lower` in Python; `String#downcase` in Ruby; `strtolower` in PHP; `lc` in Perl | |
| 101 |
| [ToLowerSpecial](http://golang.org/pkg/strings/#ToLowerSpecial) | - | |
| 102 |
| [ToTitle](http://golang.org/pkg/strings/#ToTitle) | - | |
| 103 |
| [ToTitleSpecial](http://golang.org/pkg/strings/#ToTitleSpecial) | - | |
| 104 |
| [ToUpper](http://golang.org/pkg/strings/#ToUpper) | `str.upper` in Python; `String#upcase` in Ruby; `strtoupper` in PHP; `uc` in Perl | |
| 105 |
| [ToUpperSpecial](http://golang.org/pkg/strings/#ToUpperSpecial) | - | |
| 106 |
| [Trim](http://golang.org/pkg/strings/#Trim) | `str.strip` in Python; `String#strip` in Ruby; `trim` in PHP | |
| 107 |
| [TrimFunc](http://golang.org/pkg/strings/#TrimFunc) | - | |
| 108 |
| [TrimLeft](http://golang.org/pkg/strings/#TrimLeft) | `str.lstrip` in Python; `String#lstrip` in Ruby; `ltrim` in PHP | |
| 109 |
| [TrimLeftFunc](http://golang.org/pkg/strings/#TrimLeftFunc) | - | |
| 110 |
| [TrimPrefix](http://golang.org/pkg/strings/#TrimPrefix) | - | |
| 111 |
| [TrimRight](http://golang.org/pkg/strings/#TrimRight) | `str.rstrip` in Python; `String#rstrip` in Ruby; `rtrim` in PHP | |
| 112 |
| [TrimRightFunc](http://golang.org/pkg/strings/#TrimRightFunc) | - | |
| 113 |
| [TrimSpace](http://golang.org/pkg/strings/#TrimSpace) | `str.strip` in Python; `String#strip` in Ruby; `trim` in PHP | |
| 114 |
| [TrimSuffix](http://golang.org/pkg/strings/#TrimSuffix) | `String#chomp` in Ruby; `chomp` in Perl | |
| 115 |
|
| 116 |
## License |
| 117 |
|
| 118 |
This library is licensed under MIT license. See LICENSE for details. |