stringutil

package module
v0.0.0-...-650d35b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2015 License: BSD-2-Clause Imports: 3 Imported by: 3

README

#stringutil Build Status GoDoc Readability Ruby's string manipulation magic brought to Golang

Why?

"What was the biggest surprise you encountered rolling out Go?" I knew the answer instantly: Although we expected C++ programmers to see Go as an alternative, instead most Go programmers come from languages like Python and Ruby. Robert Pike, Less is exponentially more

stringutil makes all the string manipulation methods from ruby accessible in go.

##Install

 go get https://github.com/wallclockbuilder/stringutil

##Import

 import "stringutil" "https://github.com/wallclockbuilder/stringutil"

Example

I want to remove the trailing space from the end of a string.

This is what Go forces me to do:

strings.TrimRightFunc(s, unicode.IsSpace)

This is the simple version from Ruby:

stringutil.Rstrip(s)

I prefer the Ruby version.

##Use

package main
import "github.com/wallclockbuilder/stringutil"

func main() {
  stringutil.Capitalize("abcde")          #=> "ABCDE"
  stringutil.Reverse("stressed")          #=> "desserts"
  stringutil.Swapcase("Hello")            #=> "hELLO"
}

##Documentation go doc style documentation for this package is available online at http://godoc.org/github.com/wallclockbuilder/stringutil

Contributing

  1. Fork it ( https://github.com/wallclockbuilder/stringutil )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Documentation

Overview

Package stringutil provides common sense string manipulation methods from ruby.

How to use:

package main
import "github.com/wallclockbuilder/stringutil"

func main() {
  stringutil.Capitalize("abcde")          #=> "ABCDE"
  stringutil.Reverse("stressed")          #=> "desserts"
  stringutil.Swapcase("Hello")            #=> "hELLO"
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Capitalize

func Capitalize(s string) string

Capitalize returns a new string with the first character converted to uppercase and the remainder to lowercase. Note: case conversion is effective only in ASCII region.

Example
fmt.Println(Capitalize("bare necessities"))
Output:

BARE NECESSITIES

func Chomp

func Chomp(s string, sep string) string

Chomp Returns a new String with the given record separator(sep) removed from the end of string(s) if present. Chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n). If sep is an empty string, it will remove all trailing newlines from the string.

Example
fmt.Println(Chomp("hello", "llo"))
fmt.Println(Chomp("hello\n", ""))
fmt.Println(Chomp("hello\r\n", ""))
Output:

he
hello
hello

func Chop

func Chop(s string) string

Chop returns a new String with the last character removed. If the string ends with \r\n, both characters are removed. Applying chop to an empty string returns an empty string.

Example
fmt.Println(Chop("Keep it simple"))
fmt.Println(Chop("Keep it simple\n"))
fmt.Println(Chop("Developer happiness FTW\r\n"))
Output:

Keep it simpl
Keep it simple
Developer happiness FTW

func Chr

func Chr(s string) string

Chr Returns the one-character string at the beginning of the string s.

Example
fmt.Println(Chr("abcde"))
Output:

a

func Downcase

func Downcase(s string) string

Downcase returns a copy of str with all uppercase letters replaced with their lowercase counterparts.

Example
fmt.Println(Downcase("hEllO"))
Output:

hello

func Empty

func Empty(s string) bool

Empty returns true if s has a length of zero.

Example
fmt.Println(Empty("hello"))
fmt.Println(Empty(" "))
fmt.Println(Empty(""))
Output:

false
false
true

func Include

func Include(s string, substr string) bool

Include returns true if str contains the given string or character.

Example
fmt.Println(Include("hello", "lo"))
fmt.Println(Include("hello", "ol"))
fmt.Println(Include("hello", "?h"))
Output:

true
false
true

func Length

func Length(s string) int

Length returns the character length of s.

Example
fmt.Println(Length("hello"))
Output:

5

func Lstrip

func Lstrip(s string) string

Lstrip returns a copy of s with leading whitespace removed. See also stringutil.Rstrip and stringutil.Strip

func Prepend

func Prepend(s string, item string) string

Prepend returns a new string with item at the head of s.

Example
fmt.Println(Prepend("planet", "capt. "))
Output:

capt. planet

func Reverse

func Reverse(s string) string

Reverse returns a new string with the characters from s in reverse order.

Example
fmt.Println(Reverse("yo"))
fmt.Println(Reverse("dog"))
fmt.Println(Reverse("stressed"))
Output:

oy
god
desserts

func Rstrip

func Rstrip(s string) string

Rstrip returns a copy of s with trailing whitespace removed. See also stringutil.Lstrip and stringutil.Strip

Example
fmt.Println(Rstrip("   hello   "))
Output:

hello

func Size

func Size(s string) int

Size returns number of characters in s

Example
fmt.Println(Size("hello"))
Output:

5

func Start_with

func Start_with(s string, prefix string) bool

Start_with returns true if s starts the prefix given.

Example
fmt.Println(Start_with("hello", "hell"))
Output:

true

func Strip

func Strip(s string) string

Strip returns a copy of str with leading and trailing whitespace removed.

Example
fmt.Println(Strip("   hello   "))
fmt.Println(Strip("\tgoodbye\r\n"))
Output:

hello
goodbye

func Swapcase

func Swapcase(s string) string

Swapcase returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase. Note: case conversion is effective only in ASCII region.

Example
fmt.Println(Swapcase("Hello"))
fmt.Println(Swapcase("cYbEr_PuNk11"))
fmt.Println(Swapcase("abcdefghijklmnopqrstuvwxyz"))
fmt.Println(Swapcase("developer HAPPINESS"))
Output:

hELLO
CyBeR_pUnK11
ABCDEFGHIJKLMNOPQRSTUVWXYZ
DEVELOPER happiness

func Upcase

func Upcase(s string) string

Upcase returns a copy of string s with all lowercase letters replaced with their uppercase counterparts.

Example
fmt.Println(Upcase("hEllO"))
Output:

HELLO

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL