Convert string to camel case, snake case, kebab case / slugify, custom delimiter, pad string, tease string and many other functionalities with help of by Stringy package.

Overview

Golang String manipulation helper package

Build Go Report Card GoDoc Coverage Status

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.

Between Boolean CamelCase
ContainsAll Delimited Get
KebabCase LcFirst Lines
Pad RemoveSpecialCharacter ReplaceFirst
ReplaceLast Reverse Shuffle
Surround SnakeCase Tease
ToLower ToUpper UcFirst
First Last Prefix
Suffix

Why?

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.

package main

import (
	"fmt"
	"github.com/gobeam/stringy"
    )

func main() {
 str := stringy.New("hello__man how-Are you??")
 result := str.CamelCase("?", "")
 fmt.Println(result) // HelloManHowAreYou

 snakeStr := str.SnakeCase("?", "")
 fmt.Println(snakeStr.ToLower()) // hello_man_how_are_you

 kebabStr := str.KebabCase("?", "")
 fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
}

Installation

$ go get -u -v github.com/gobeam/stringy

or with dep

$ dep ensure -add github.com/gobeam/stringy

Functions

Between(start, end string) StringManipulation

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.

  strBetween := stringy.New("HelloMyName")
  fmt.Println(strBetween.Between("hello", "name").ToUpper()) // MY

Boolean() bool

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.

  boolString := stringy.New("off")
  fmt.Println(boolString.Boolean()) // false

CamelCase(rule ...string) string

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.

  camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
  fmt.Println(camelCase.CamelCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt

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.

  camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
  fmt.Println(camelCase.CamelCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?##

ContainsAll(check ...string) bool

ContainsAll is variadic function which takes slice of strings as param and checks if they are present in input and returns boolean value accordingly.

  contains := stringy.New("hello mam how are you??")
  fmt.Println(contains.ContainsAll("mam", "?")) // true

Delimited(delimiter string, rule ...string) StringManipulation

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.

  delimiterString := stringy.New("ThisIsOne___messed up string. Can we Really delimeter-case It?")
  fmt.Println(delimiterString.Delimited("?").Get())

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.

  fcn := stringy.New("4111 1111 1111 1111")
  first := fcn.First(4)
  fmt.Println(first) // 4111

Get() string

Get simply returns result and can be chained on function which returns StringManipulation interface view above examples

KebabCase(rule ...string) StringManipulation

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.

  str := stringy.New("hello__man how-Are you??")
  kebabStr := str.KebabCase("?","")
  fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
  fmt.Println(kebabStr.Get()) // hello-man-how-Are-you

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.

  lcn := stringy.New("4111 1111 1111 1348")
  last := lcn.Last(4)
  fmt.Println(last) // 1348

LcFirst() string

LcFirst simply returns result by lower casing first letter of string and it can be chained on function which return StringManipulation interface

  contains := stringy.New("Hello roshan")
  fmt.Println(contains.LcFirst()) // hello roshan

Lines() []string

Lines returns slice of strings by removing white space characters

  lines := stringy.New("fòô\r\nbàř\nyolo123")
  fmt.Println(lines.Lines()) // [fòô bàř yolo123]

Pad(length int, with, padType string) string

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

  pad := stringy.New("Roshan")
  fmt.Println(pad.Pad(0, "0", "both"))  // 00Roshan00
  fmt.Println(pad.Pad(0, "0", "left"))  // 0000Roshan
  fmt.Println(pad.Pad(0, "0", "right")) // Roshan0000

RemoveSpecialCharacter() string

RemoveSpecialCharacter removes all special characters and returns the string nit can be chained on function which return StringManipulation interface

  cleanString := stringy.New("[email protected]#remove%%%%")
  fmt.Println(cleanString.RemoveSpecialCharacter()) // specialremove

ReplaceFirst(search, replace string) string

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.

  replaceFirst := stringy.New("Hello My name is Roshan and his name is Alis.")
  fmt.Println(replaceFirst.ReplaceFirst("name", "nombre")) // Hello My nombre is Roshan and his name is Alis.

ReplaceLast(search, replace string) string

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

  replaceLast := stringy.New("Hello My name is Roshan and his name is Alis.")
  fmt.Println(replaceLast.ReplaceLast("name", "nombre")) // Hello My name is Roshan and his nombre is Alis.

Reverse() string

Reverse function reverses the passed strings it can be chained on function which return StringManipulation interface.

  reverse := stringy.New("This is only test")
  fmt.Println(reverse.Reverse()) // tset ylno si sihT

Shuffle() string

Shuffle shuffles the given string randomly it can be chained on function which return StringManipulation interface.

  shuffleString := stringy.New("roshan")
  fmt.Println(shuffleString.Shuffle()) // nhasro

Surround(with string) string

Surround takes one param with which is used to surround user input and it can be chained on function which return StringManipulation interface.

  surroundStr := stringy.New("__")
  fmt.Println(surroundStr.Surround("-")) // -__-

SnakeCase(rule ...string) StringManipulation

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.

  snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
  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

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.

Tease(length int, indicator string) string

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.

  teaseString := stringy.New("Hello My name is Roshan. I am full stack developer")
  fmt.Println(teaseString.Tease(20, "...")) // Hello My name is Ros...

ToLower() string

ToLower makes all string of user input to lowercase and it can be chained on function which return StringManipulation interface.

  snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
  fmt.Println(snakeCase.SnakeCase("?", "").ToLower()) // this_is_one_messed_up_string_can_we_really_snake_case_it

ToUpper() string

ToUpper makes all string of user input to uppercase and it can be chained on function which return StringManipulation interface.

  snakeCase := stringy.New("ThisIsOne___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

UcFirst() string

LcFirst simply returns result by lower casing first letter of string and it can be chained on function which return StringManipulation interface.

  contains := stringy.New("hello roshan")
  fmt.Println(contains.UcFirst()) // Hello roshan

Prefix(string) string

Prefix makes sure string has been prefixed with a given string and avoids adding it again if it has.

  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.

  pun := stringy.New("this really is a cliff")
  fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger

Running the tests

$ 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.

License

Released under the MIT License - see LICENSE.txt for details.

Issues
  • Add method to uppercase each word

    Add method to uppercase each word

    Since this library wraps a lot of built-in functions, I miss this one(essentially a snake case but with spaces): https://pkg.go.dev/strings#Title

    opened by ivanjaros 0
Releases(v0.0.5)
  • v0.0.5(Nov 9, 2021)

    What's Changed

    • Fix LcFirst bug with leading multi-byte character. Addresses #8 by @mattcarmody in https://github.com/gobeam/stringy/pull/9
    • UcFirst accounts for multi-byte leading characters. Addresses #10 by @mattcarmody in https://github.com/gobeam/stringy/pull/11

    New Contributors

    • @mattcarmody made their first contribution in https://github.com/gobeam/stringy/pull/9

    Full Changelog: https://github.com/gobeam/stringy/compare/v0.0.4...v0.0.5

    Source code(tar.gz)
    Source code(zip)
Owner
Roshan Ranabhat
Full Stack Developer (Golang, Node.js, PHP, React & Redux, Vue.js)
Roshan Ranabhat
Snake game made in Go! 🐍

Snake This is a Terminal based snake game made by tristangoossens. Please star this repository to help my first big project grow! Documentation can be

Tristan Goossens 308 Jan 14, 2022
Snake game implemented in golang

little_pineapple(Snake game implemented in golang) 贪吃蛇golang实现 Snake game implemented in golang 数据结构:链表&数组 Data structures used: linked list&array 使用方

null 7 Aug 18, 2020
A simple desktop snake game

A simple desktop snake game that's supposed to run on Linux and Mac OS (maybe eventually on Windows). Work on the game is currently in progress - so i

Meghashyam Chirravoori 2 Oct 12, 2021
snake game implementation using 2d array in Go

Snake Game Implementation Snake game implementation in Go using a 2-dimensional array. Demo Install download the package git clone https://github.com/

Iss Meftah 8 Nov 25, 2021
Command line Snake Game built with Golang

Commandline Snake Game Commandline Snake Game built with Golang. Took about a whole day to complete. Had breaks inbetween progress made. Assumptions H

Jubril Aminu 0 Dec 5, 2021
A golang Snake game in webassembly

snake A simple Snake game in Go WebAssembly, loading each grid tile from a diffe

Brad Fitzpatrick 91 Jan 16, 2022
The simple and light self-hosted custom event analytics you were looking for.

Dead-Simple-Game-Analytics What is Dead Simple Game Analytics? An extremely simple self-hosted system to log your game analytics events. No fluff or o

Nesh 1 Jan 3, 2022
basicbots is a programming game where you program your robot to compete against other robot.

basicbots What is baiscbots? basicbots is a programming game where you program your robot to compete agaist other robots. What is the inspiration? My

Bill Jones 23 Jan 8, 2022
A performance oriented 2D/3D math package for Go

Package go3d is a performance oriented vector and matrix math package for 2D and 3D graphics. Every type has its own sub-package and is named T. So ve

Erik Unger 218 Jan 16, 2022
chess package for go

chess Introduction chess is a set of go packages which provide common chess utilities such as move generation, turn management, checkmate detection, P

Logan Spears 336 Jan 7, 2022
Scalable game server framework with clustering support and client libraries for iOS, Android, Unity and others through the C SDK.

pitaya Pitaya is an simple, fast and lightweight game server framework with clustering support and client libraries for iOS, Android, Unity and others

TFG Co 1.3k Jan 20, 2022
Tile is a 2D grid engine, built with data and cache friendly ways, includes pathfinding and observers.

Tile: Data-Oriented 2D Grid Engine This repository contains a 2D tile map engine which is built with data and cache friendly ways. My main goal here i

Roman Atachiants 40 Jan 15, 2022
Tetra3D is a 3D software renderer written in Go and Ebiten and made for games.

Tetra3D Tetra3D Docs Support If you want to support development, feel free to check out my itch.io / Steam / Patreon. I also have a Discord server her

SolarLune 143 Jan 21, 2022
an API to handle the deck and cards to be used in any game like Poker and Blackjack.

Card Games REST API in GO Simple RESTful API for Card games. with database implementation Quick Start # Install mux router cd /main go get -u github.c

AhmedHesham 0 Dec 22, 2021
Go bindings for raylib, a simple and easy-to-use library to enjoy videogames programming.

raylib-go Golang bindings for raylib, a simple and easy-to-use library to enjoy videogames programming. Requirements Ubuntu X11 apt-get install libgl1

Milan Nikolic 714 Jan 8, 2022
Play chess with Go, HTML5, WebSockets and random strangers!

ChessBuddy Play chess with Go, HTML5, WebSockets and random strangers! Demo: http://chess.tux21b.org:8000/ Hint: Open the page in two different tabs,

Christoph Hack 113 Nov 22, 2020
An implementation of the popular game Codenames created with Go and React.

OpenCodenames A real-time implementation of Codenames created with React/TypeScript and Golang. You can play the game here! Installation Stack: React

null 29 Aug 8, 2021
Dedicated Game Server Hosting and Scaling for Multiplayer Games on Kubernetes

Agones is a library for hosting, running and scaling dedicated game servers on Kubernetes. Agones, is derived from the Greek word agōn which roughly t

GoogleForGames 4.3k Jan 19, 2022
A game server side framework with both web API and realtime communication.

HAYABUSA Framework Hayabusa is a server side framework for Japan-like social games. Easy to understand and use for beginners Powerful controller, flex

はやぶさクラウド 21 Jan 13, 2022