Implements string functions widely used in other languages but absent in Go.

Overview

xstrings

Build Status Go Doc Go Report Coverage Status

Go package xstrings is a collection of string functions, which are widely used in other languages but absent in Go package strings.

All functions are well tested and carefully tuned for performance.

Propose a new function

Please review contributing guideline and create new issue to state why it should be included.

Install

Use go get to install this library.

go get github.com/huandu/xstrings

API document

See GoDoc for full document.

Function list

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.

Here is a list of functions in strings and 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.

Package xstrings functions

Keep this table sorted by Function in ascending order.

Function Friends #
Center str.center in Python; String#center in Ruby #30
Count String#count in Ruby #16
Delete String#delete in Ruby #17
ExpandTabs str.expandtabs in Python #27
FirstRuneToLower lcfirst in PHP or Perl #15
FirstRuneToUpper String#capitalize in Ruby; ucfirst in PHP or Perl #15
Insert String#insert in Ruby #18
LastPartition str.rpartition in Python; String#rpartition in Ruby #19
LeftJustify str.ljust in Python; String#ljust in Ruby #28
Len mb_strlen in PHP #23
Partition str.partition in Python; String#partition in Ruby #10
Reverse String#reverse in Ruby; strrev in PHP; reverse in Perl #7
RightJustify str.rjust in Python; String#rjust in Ruby #29
RuneWidth - #27
Scrub String#scrub in Ruby #20
Shuffle str_shuffle in PHP #13
ShuffleSource str_shuffle in PHP #13
Slice mb_substr in PHP #9
Squeeze String#squeeze in Ruby #11
Successor String#succ or String#next in Ruby #22
SwapCase str.swapcase in Python; String#swapcase in Ruby #12
ToCamelCase String#camelize in RoR #1
ToKebab - #41
ToSnakeCase String#underscore in RoR #1
Translate str.translate in Python; String#tr in Ruby; strtr in PHP; tr/// in Perl #21
Width mb_strwidth in PHP #26
WordCount str_word_count in PHP #14
WordSplit - #14

Package strings functions

Keep this table sorted by Function in ascending order.

Function Friends
Contains String#include? in Ruby
ContainsAny -
ContainsRune -
Count str.count in Python; substr_count in PHP
EqualFold stricmp in PHP; String#casecmp in Ruby
Fields str.split in Python; split in Perl; String#split in Ruby
FieldsFunc -
HasPrefix str.startswith in Python; String#start_with? in Ruby
HasSuffix str.endswith in Python; String#end_with? in Ruby
Index str.index in Python; String#index in Ruby; strpos in PHP; index in Perl
IndexAny -
IndexByte -
IndexFunc -
IndexRune -
Join str.join in Python; Array#join in Ruby; implode in PHP; join in Perl
LastIndex str.rindex in Python; String#rindex; strrpos in PHP; rindex in Perl
LastIndexAny -
LastIndexFunc -
Map String#each_codepoint in Ruby
Repeat operator * in Python and Ruby; str_repeat in PHP
Replace str.replace in Python; String#sub in Ruby; str_replace in PHP
Split str.split in Python; String#split in Ruby; explode in PHP; split in Perl
SplitAfter -
SplitAfterN -
SplitN str.split in Python; String#split in Ruby; explode in PHP; split in Perl
Title str.title in Python
ToLower str.lower in Python; String#downcase in Ruby; strtolower in PHP; lc in Perl
ToLowerSpecial -
ToTitle -
ToTitleSpecial -
ToUpper str.upper in Python; String#upcase in Ruby; strtoupper in PHP; uc in Perl
ToUpperSpecial -
Trim str.strip in Python; String#strip in Ruby; trim in PHP
TrimFunc -
TrimLeft str.lstrip in Python; String#lstrip in Ruby; ltrim in PHP
TrimLeftFunc -
TrimPrefix -
TrimRight str.rstrip in Python; String#rstrip in Ruby; rtrim in PHP
TrimRightFunc -
TrimSpace str.strip in Python; String#strip in Ruby; trim in PHP
TrimSuffix String#chomp in Ruby; chomp in Perl

License

This library is licensed under MIT license. See LICENSE for details.

Issues
  • Added code to run PowerSystems

    Added code to run PowerSystems

    Hi, This is my contribution to your code to make it work on PowerSystems.

    Thanks for the code, its working good.

    What do these changes do?

    Added Architecture "ppc64le"

    Are there changes in behavior for the user?

    No

    invalid Enhancement 
    opened by genisysram 4
  • Use strings builder

    Use strings builder

    Hello! I'm suggesting to use the strings.Builder instead of bytes.Buffer. It's a more efficient alternative with very low cast costs.

    Enhancement 
    opened by ninedraft 4
  • Feature: slice versions

    Feature: slice versions

    Is there any plan to add functions to operate on string slice, instead of one string?

    question 
    opened by ferhatelmas 3
  • Consider semantic versioning

    Consider semantic versioning

    Hello and thanks for package!

    Please consider semantic versioning tags in this project (https://semver.org). That'd make life of dependency management tools users easier.

    Thanks!

    question 
    opened by kron4eg 3
  • Add some common string constants

    Add some common string constants

    Despite the fact that the unicode package allows you to get almost all unicode ranges, it can be somewhat cumbersome to use. In addition, its use forces the inclusion of a large symbol table in the executable file. At the same time, there is a set of characters that are most often found in text data formats. I suggest adding constant strings with these characters, similar to the way it is done in the python standard library.

    New API/Feature wontfix 
    opened by ninedraft 3
  • Feature: Camel case from/to underscore conversion

    Feature: Camel case from/to underscore conversion

    Camel case to underscore.

    • SomeWords => some_words
    • HTTPServer => http_server
    • NoHTTPS => no_https

    Underscore to camel case.

    • some_words => SomeWords
    • http_server => HttpServer
    • no_https => NoHttps
    • _complex__case_ => _Complex_Case_
    New API/Feature 
    opened by huandu 3
  • Release new version

    Release new version

    Can there be a new version released that includes the kebab strings feature?

    It's used here: https://github.com/Masterminds/sprig/blob/9f8fceff796fb9f4e992cd2bece016be0121ab74/functions.go#L132

    And I'm seeing some cases where Go Modules users get install failures because v1.0.0 of this repo is being installed.

    question 
    opened by rarkins 2
  • Feature: Hyphen case

    Feature: Hyphen case

    Hyphen case is similar to snake case. All alphabets are lower case and words are connected with hyphens.

    New API/Feature 
    opened by huandu 2
  • CamelCase and capital input

    CamelCase and capital input

    Hi,

    func TestToCamelCase(t *testing.T) {
    	runTestCases(t, ToCamelCase, _M{
    		"GOLANG":             "Golang",
    	})
    }
    

    The following test should succeed instead it returns "GOLANG".

    --- FAIL: TestToCamelCase (0.00s)
            util_test.go:22: case fails. [case:GOLANG]
                    should => "Golang"
                    actual => "GOLANG"
    

    Is it a wanted behavior?

    Cheers,

    bug 
    opened by jhayotte 2
Releases(v1.3.2)
Owner
Huan Du
I'm a software developer from China. I feel very satisfied when other developers use my code to solve their own problems.
Huan Du
Stalin sort in multiple languages!

stalin-sort Stalin sort in multiple languages, contributions are welcome! Motivation This repo is motivated by this tweet, this tweet contains a refer

CaKrome 0 Jan 14, 2022
Package mafsa implements Minimal Acyclic Finite State Automata in Go, essentially a high-speed, memory-efficient, Unicode-friendly set of strings.

MA-FSA for Go Package mafsa implements Minimal Acyclic Finite State Automata (MA-FSA) with Minimal Perfect Hashing (MPH). Basically, it's a set of str

SmartyStreets (Archives) 285 Jan 16, 2022
Golang string comparison and edit distance algorithms library, featuring : Levenshtein, LCS, Hamming, Damerau levenshtein (OSA and Adjacent transpositions algorithms), Jaro-Winkler, Cosine, etc...

Go-edlib : Edit distance and string comparison library Golang string comparison and edit distance algorithms library featuring : Levenshtein, LCS, Ham

Hugo Bollon 300 Jan 11, 2022
Multi-String Pattern Matching Algorithm Using TrieHashNode

Multi-String Pattern Matching algorithm. This implementation is inspired from Aho-Corasick algorithm Getting Started modelA = mspm.NewModel("mspm_mode

Sujit Shakya 16 Jul 12, 2021
Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching.

Trie Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching. Usage Create a Trie with: t := trie.New() Add Keys with:

Derek Parker 569 Jan 20, 2022
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Rene Kroon 333 Jan 16, 2022
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Charles Banning 499 Jan 12, 2022
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Rene Kroon 333 Jan 16, 2022
Implementation of Boyer-Moore fast string search algorithm in Go

boyermoore Implementation of Boyer-Moore fast string search algorithm in Go

sarp dağ demirel 47 Dec 1, 2021
A fast (5x) string keyed read-only map for Go - particularly good for keys using a small set of nearby runes.

faststringmap faststringmap is a fast read-only string keyed map for Go (golang). For our use case it is approximately 5 times faster than using Go's

The Sensible Code Company 23 Dec 2, 2021
Convert json string to Golang struct

json-to-go-cli Convert json string to Golang struct How to install git clone https://github.com/tiancheng92/json-to-go-cli.git cd json-to-go-cli go bu

TianCheng 6 Nov 15, 2021
Set is a useful collection but there is no built-in implementation in Go lang.

goset Set is a useful collection but there is no built-in implementation in Go lang. Why? The only one pkg which provides set operations now is golang

zoumo 43 Nov 25, 2021
Brushing questions is not the goal, but the mastering method is

Brushing questions is not the goal, but the mastering method is If you think it

null 2 Jan 8, 2022
A threadsafe single-value cache for Go with a simple but flexible API

SVCache SVCache is a threadsafe, single-value cache with a simple but flexible API. When there is no fresh value in the cache, an attempt to retrieve

softwaretechnik.berlin 0 Jan 10, 2022
Data structure and algorithm library for go, designed to provide functions similar to C++ STL

GoSTL English | 简体中文 Introduction GoSTL is a data structure and algorithm library for go, designed to provide functions similar to C++ STL, but more p

stirlingx 362 Jan 13, 2022
Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

nan - No Allocations Nevermore Package nan - Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmar

Andrey Kuzmin 46 Dec 20, 2021
Find functions that return a reference and cause allocations.

refreturn Find functions that return a reference and cause allocations. When a function allocates a value and returns a reference to it, the value has

Dominik Braun 26 Jan 9, 2021
A package for Go that can be used for range queries on large number of intervals

go-stree go-stree is a package for Go that can be used to process a large number of intervals. The main purpose of this module is to solve the followi

Thomas Oberndörfer 38 Oct 25, 2021
estruct traverses javascript projects and maps all the dependencies and relationships to a JSON. the output can be used to build network visualizations of the project and document the architecture.

EStruct traverses javascript projects and maps all the dependencies and relationships to a JSON. The output can be used to build network visualizations of the project and document the architecture.

Ray Luxembourg 11 Sep 30, 2021