strings

package
v0.0.0-...-82e7740 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 4 Imported by: 44

README

util-strings GoDoc

string library for Go

Installation

go get gopkg.in/goyy/goyy.v0/util/strings

Usage

IsBlank

fmt.Println(strings.IsBlank(""))
fmt.Println(strings.IsBlank("  "))
fmt.Println(strings.IsBlank(" \t\r\n "))
fmt.Println(strings.IsBlank("a"))
fmt.Println(strings.IsBlank(" a  "))
fmt.Println(strings.IsBlank(" \t\r\n a \t\r\n "))
// Output:
// true
// true
// true
// false
// false
// false

IsNotBlank

fmt.Println(strings.IsNotBlank(""))
fmt.Println(strings.IsNotBlank("  "))
fmt.Println(strings.IsNotBlank(" \t\r\n "))
fmt.Println(strings.IsNotBlank("a"))
fmt.Println(strings.IsNotBlank(" a  "))
fmt.Println(strings.IsNotBlank(" \t\r\n a \t\r\n "))
// Output:
// false
// false
// false
// true
// true
// true

Left

fmt.Println(strings.Left("abc", 2))
fmt.Println(strings.Left("abc", 4))
// Output:
// ab
// abc

Right

fmt.Println(strings.Right("abc", 2))
fmt.Println(strings.Right("abc", 4))
// Output:
// bc
// abc

Mid

fmt.Println(strings.Mid("abc", 0, 2))
fmt.Println(strings.Mid("abc", 0, 4))
fmt.Println(strings.Mid("abc", -2, 2))
// Output:
// ab
// abc
// bc

Before

fmt.Println(strings.Before("abc", "c"))
fmt.Println(strings.Before("abcba", "b"))
// Output:
// ab
// a

After

fmt.Println(strings.After("abc", "a"))
fmt.Println(strings.After("abcba", "b"))
// Output:
// bc
// cba

BeforeLast

fmt.Println(strings.BeforeLast("abc", "c"))
fmt.Println(strings.BeforeLast("abcba", "b"))
// Output:
// ab
// abc

AfterLast

fmt.Println(strings.AfterLast("abc", "a"))
fmt.Println(strings.AfterLast("abcba", "b"))
// Output:
// bc
// a

Between

fmt.Println(strings.Between("yabcz", "y", "z"))
fmt.Println(strings.Between("yabczydefz", "y", "z"))
// Output:
// abc
// abc

BetweenSame

fmt.Println(strings.BetweenSame("tagabctag", "tag"))
// Output: abc

Repeat

fmt.Println(strings.Repeat("-", 10))
// Output: ----------

PadStart

fmt.Printf("%#q\n", strings.PadStart("bat", 5))
// Output: `  bat`

PadEnd

fmt.Printf("%#q\n", strings.PadEnd("bat", 5))
// Output: `bat  `

PadLeft

fmt.Println(strings.PadLeft("bat", 5, "*"))
// Output: **bat

PadRight

fmt.Println(strings.PadRight("bat", 5, "*"))
// Output: bat**

Pad

fmt.Printf("%#q\n", strings.Pad("bat", 5))
// Output: ` bat `

Documentation

Overview

Package strings operations on string: 1.IsBlank/IsNotBlank/IsAnyBlank/IsNoneBlank - checks if a String contains text 2.Trim/TrimLeft/TrimRight/TrimSpace - removes leading and trailing whitespace 3.Index/IndexLast/IndexLastStart/IndexLastOrdinal/IndexStart/IndexOrdinal - index checks 4.Contains/ContainsAny/ContainsRune/ContainsSpace - does String contains only/none/any of these characters 5.EqualFold - compares two strings 6.Slice/Left/Right/Mid/Before/After/BeforeLast/AfterLast/Between/BetweenSame - substring extractions 7.Split/SplitAfter/SplitAfterN/SplitN - splits a String into an array of substrings 8.Remove/RemoveSpace/RemoveStart/RemoveEnd - removes part of a string 9.Repeat/Pad/PadStart/PadEnd/PadLeft/PadRight/Center - pads a string 10.ToLower/ToUpper/Title/ToTitle/Camel/UnCamel - changes the case of a string 11.Abbr/Anon/Anonymity/Anonymous - abbreviates a string using ellipsis or asterisk

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abbr

func Abbr(s string, maxWidth int) string

Abbr abbreviate a String using ellipses. This will turn "goyy" into "g...".

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []struct {
		in    string
		width int
	}{
		{"", 6},
		{"a", 6},
		{"abc", 6},
		{"AbC123", 6},
		{"azAZ09_", 6},
		{"世界hello", 6},
	}
	for _, v := range s {
		fmt.Println(strings.Abbr(v.in, v.width))
	}

}
Output:


a
abc
AbC123
azA...
世界h...

func After

func After(s, sep string) string

After gets the substring after the first occurrence of a separator. The separator is not returned.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.After("abc", "a"))
	fmt.Println(strings.After("abcba", "b"))
	fmt.Println(strings.After("abcba", "e"))

}
Output:

bc
cba

func AfterLast

func AfterLast(s, sep string) string

AfterLast gets the substring after the last occurrence of a separator. The separator is not returned.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.AfterLast("abc", "a"))
	fmt.Println(strings.AfterLast("abcba", "b"))

}
Output:

bc
a

func Anon

func Anon(s string) string

Anon anonymous a String using asterisk. This will turn "goyy" into "g***y".

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"", "a", "ac", "abc", "AbC123", "azAZ09_", "世界hello"}
	for _, v := range s {
		fmt.Println(strings.Anon(v))
	}

}
Output:


a
a***c
a***c
A***3
a***_
世***o

func Anonymity

func Anonymity(s string) string

Anonymity a String using asterisk. This will turn "15566668888" into "155****8888".

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"", "abc", "15566668888", "abcdefghijk", "abcdefghijklmn"}
	for _, v := range s {
		fmt.Println(strings.Anonymity(v))
	}

}
Output:


abc
155****8888
abc****hijk
abc****klmn

func Anonymous

func Anonymous(s string, leftLen, rightLen, starLen int) string

Anonymous a String using asterisk. This will turn "goyy" into "g**y".

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []struct {
		in                string
		left, right, star int
	}{
		{"", 1, 1, 0},
		{"a", -1, -1, 10},
		{"abc", -1, -1, 3},
		{"abcd", 5, 5, 3},
		{"AbC123", 1, 1, 0},
		{"azAZ09_", 1, 1, 3},
		{"世界hello", 1, 2, 3},
	}
	for _, v := range s {
		fmt.Println(strings.Anonymous(v.in, v.left, v.right, v.star))
	}

}
Output:


a
a***c
abcd
A****3
a***_
世***lo

func Before

func Before(s, sep string) string

Before gets the substring before the first occurrence of a separator. The separator is not returned.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Before("abc", "c"))
	fmt.Println(strings.Before("abcba", "b"))

}
Output:

ab
a

func BeforeLast

func BeforeLast(s, sep string) string

BeforeLast gets the substring before the last occurrence of a separator. The separator is not returned.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.BeforeLast("abc", "c"))
	fmt.Println(strings.BeforeLast("abcba", "b"))

}
Output:

ab
abc

func Between

func Between(s, start, end string) string

Between gets the string that is nested in between two string. Only the first match is returned.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Between("yabcz", "y", "z"))
	fmt.Println(strings.Between("yabczydefz", "y", "z"))

}
Output:

abc
abc

func BetweenSame

func BetweenSame(s, tag string) string

BetweenSame gets the string that is nested in between two instances of the same String.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.BetweenSame("tagabctag", "tag"))

}
Output:

abc

func Betweens

func Betweens(s, start, end string) (out []string)

Betweens gets the slice string that is nested in between two string. Only the first match is returned. eg. strings.Betweens("[a][b][c]", "[", "]") = [a b c] strings.Betweens("1(aa)2(bb)3(cc)4", "{", "}") = [aa bb cc] strings.Betweens(" 1${id}2 3${name} ${pwd}4", "${", "}") = [id name pwd]

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Betweens("[a][b][c]", "[", "]"))
	fmt.Println(strings.Betweens("1(aa)2(bb)3(cc)4", "(", ")"))

}
Output:

[a b c]
[aa bb cc]

func Camel

func Camel(s string) string

Camel returns a copy of the string s with all Unicode letters that begin words mapped to their camel case.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"",
		"a",
		"cat",
		"cAt",
		" aaa aaa aaa ",
		"_Aaa_Aaa_Aaa_",
		"123a456",
		"douBle-blind",
		"ÿøû"}
	for _, v := range s {
		fmt.Println(strings.Camel(v))
	}

}
Output:


A
Cat
Cat
AaaAaaAaa
AaaAaaAaa
123a456
DoubleBlind
Ÿøû

func Center

func Center(in string, size int, sep string) (out string)

Center centers a string in a larger string of size.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Center("bat", 8, "tag"))

}
Output:

tabattag

func Contains

func Contains(s, substr string) bool

Contains returns true if substr is within s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Contains("seafood", "foo"))
	fmt.Println(strings.Contains("seafood", "bar"))
	fmt.Println(strings.Contains("seafood", ""))
	fmt.Println(strings.Contains("", ""))
	fmt.Println(strings.Contains("", "foo"))

}
Output:

true
false
true
true
false

func ContainsAny

func ContainsAny(s, chars string) bool

ContainsAny returns true if any Unicode code points in chars are within s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsAny("team", "i"))
	fmt.Println(strings.ContainsAny("failure", "u & i"))
	fmt.Println(strings.ContainsAny("foo", ""))
	fmt.Println(strings.ContainsAny("", ""))

}
Output:

false
true
false
false

func ContainsNone

func ContainsNone(s, chars string) bool

ContainsNone checks that the string does not contain certain chars.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsNone("abab", "xyz"))
	fmt.Println(strings.ContainsNone("ab1", "xyz"))
	fmt.Println(strings.ContainsNone("abz", "xyz"))

}
Output:

true
true
false

func ContainsOnly

func ContainsOnly(s, chars string) bool

ContainsOnly checks if the string contains only certain chars.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsOnly("abab", "abc"))
	fmt.Println(strings.ContainsOnly("ab1", "abc"))
	fmt.Println(strings.ContainsOnly("abz", "abc"))

}
Output:

true
false
false

func ContainsRune

func ContainsRune(s string, r rune) bool

ContainsRune returns true if the Unicode code point r is within s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsRune("team", 'i'))
	fmt.Println(strings.ContainsRune("failure", 'u'))

}
Output:

false
true

func ContainsSlice

func ContainsSlice(s string, chars []string) bool

ContainsSlice returns true if chars is within s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsSlice("abcd", []string{"ab", "cd"}))
	fmt.Println(strings.ContainsSlice("ab1", []string{"ab", "12"}))
	fmt.Println(strings.ContainsSlice("abz", []string{"xy", "cd"}))
	fmt.Println(strings.ContainsSlice("abz", []string{"", ""}))
	fmt.Println(strings.ContainsSlice("", []string{"xy", "cd"}))

}
Output:

true
false
false
true
false

func ContainsSliceAny

func ContainsSliceAny(s string, chars []string) bool

ContainsSliceAny returns true if any Unicode code points in chars are within s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsSliceAny("abab", []string{"ab", "cd"}))
	fmt.Println(strings.ContainsSliceAny("ab1", []string{"abc", "12"}))
	fmt.Println(strings.ContainsSliceAny("abz", []string{"xy", "cd"}))
	fmt.Println(strings.ContainsSliceAny("abz", []string{"", ""}))
	fmt.Println(strings.ContainsSliceAny("", []string{"xy", "cd"}))

}
Output:

true
false
false
true
false

func ContainsSpace

func ContainsSpace(s string) bool

ContainsSpace contains returns true if whitespace is within s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.ContainsSpace("a"))
	fmt.Println(strings.ContainsSpace(" a "))
	fmt.Println(strings.ContainsSpace("ab c"))
	fmt.Println(strings.ContainsSpace("ab\tc"))
	fmt.Println(strings.ContainsSpace("ab\rc"))
	fmt.Println(strings.ContainsSpace("ab\nc"))

}
Output:

false
true
true
true
true
true

func Count

func Count(s, sep string) int

Count counts the number of non-overlapping instances of sep in s.

func EqualFold

func EqualFold(s, t string) bool

EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.EqualFold("Go", "go"))

}
Output:

true

func Fields

func Fields(s string) []string

Fields splits the string s around each instance of one or more consecutive white space characters, returning an array of substrings of s or an empty list if s contains only white space.

func FieldsFunc

func FieldsFunc(s string, f func(rune) bool) []string

FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned.

func FieldsSpace

func FieldsSpace(s string) []string

FieldsSpace splits the string s around each instance of one or more consecutive white space characters, returning an array of substrings of s or an empty list if s contains only white space.

func HasAnyPrefix

func HasAnyPrefix(s string, prefix ...string) (string, bool)

HasAnyPrefix tests whether the string s begins with prefix.

func HasAnySuffix

func HasAnySuffix(s string, suffix ...string) (string, bool)

HasAnySuffix tests whether the string s ends with suffix.

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix tests whether the string s begins with prefix.

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix tests whether the string s ends with suffix.

func Index

func Index(s, sep string) int

Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Index("abABabABabAB", "ab"))
	fmt.Println(strings.Index("abcdABCDabcd", "cd"))
	fmt.Println(strings.Index("abcdABCDabcd", "de"))

}
Output:

0
2
-1

func IndexAny

func IndexAny(s, chars string) int

IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.

func IndexByte

func IndexByte(s string, c byte) int

IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.

func IndexForward

func IndexForward(s, sep string, start int) int

IndexForward returns the forward index of the first instance of sep in s from a start position, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IndexForward("abABabABabAB", "ab", 6))
	fmt.Println(strings.IndexForward("abcdABCDabcd", "cd", 5))
	fmt.Println(strings.IndexForward("abcdABCDabcd", "de", 0))

}
Output:

4
2
-1

func IndexFunc

func IndexFunc(s string, f func(rune) bool) int

IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.

func IndexLast

func IndexLast(s, sep string) int

IndexLast returns the index of the last instance of sep in s, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IndexLast("abABabABabAB", "ab"))
	fmt.Println(strings.IndexLast("abcdABCDabcd", "cd"))
	fmt.Println(strings.IndexLast("abcdABCDabcd", "de"))

}
Output:

8
10
-1

func IndexLastAny

func IndexLastAny(s, chars string) int

IndexLastAny returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.

func IndexLastFunc

func IndexLastFunc(s string, f func(rune) bool) int

IndexLastFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.

func IndexLastOrdinal

func IndexLastOrdinal(s, sep string, ordinal int) int

IndexLastOrdinal returns the index of the n-th last instance of sep in s, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IndexLastOrdinal("abABabABabAB", "ab", 2))
	fmt.Println(strings.IndexLastOrdinal("abcdABCDabcd", "cd", 2))
	fmt.Println(strings.IndexLastOrdinal("abcdABCDabcd", "de", 1))

}
Output:

4
2
-1

func IndexLastStart

func IndexLastStart(s, sep string, start int) int

IndexLastStart returns the index of the last instance of sep in s from a start position, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IndexLastStart("abABabABabAB", "ab", 6))
	fmt.Println(strings.IndexLastStart("abcdABCDabcd", "cd", 5))
	fmt.Println(strings.IndexLastStart("abcdABCDabcd", "de", 0))

}
Output:

4
2
-1

func IndexOrdinal

func IndexOrdinal(s, sep string, ordinal int) int

IndexOrdinal returns the index of the n-th instance of sep in s, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IndexOrdinal("abABabABabAB", "ab", 3))
	fmt.Println(strings.IndexOrdinal("abcdABCDabcd", "cd", 2))
	fmt.Println(strings.IndexOrdinal("abcdABCDabcd", "de", 1))

}
Output:

8
10
-1

func IndexRune

func IndexRune(s string, r rune) int

IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s.

func IndexStart

func IndexStart(s, sep string, start int) int

IndexStart returns the index of the first instance of sep in s from a start position, or -1 if sep is not present in s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IndexStart("abABabABabAB", "ab", 6))
	fmt.Println(strings.IndexStart("abcdABCDabcd", "cd", 5))
	fmt.Println(strings.IndexStart("abcdABCDabcd", "de", 0))

}
Output:

8
10
-1

func IsAnyBlank

func IsAnyBlank(s ...string) bool

IsAnyBlank checks if any one of the string are empty ("") or whitespace.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IsAnyBlank("", " "))
	fmt.Println(strings.IsAnyBlank("  ", " a "))
	fmt.Println(strings.IsAnyBlank(" \t\r\n ", " a "))
	fmt.Println(strings.IsAnyBlank("a", " a "))
	fmt.Println(strings.IsAnyBlank(" a ", " a "))
	fmt.Println(strings.IsAnyBlank(" \t\r\n a \t\r\n ", " a "))

}
Output:

true
true
true
false
false
false

func IsBlank

func IsBlank(s string) bool

IsBlank checks if a string is empty ("") or whitespace.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IsBlank(""))
	fmt.Println(strings.IsBlank("  "))
	fmt.Println(strings.IsBlank(" \t\r\n "))
	fmt.Println(strings.IsBlank("a"))
	fmt.Println(strings.IsBlank(" a  "))
	fmt.Println(strings.IsBlank(" \t\r\n a \t\r\n "))

}
Output:

true
true
true
false
false
false

func IsNoneBlank

func IsNoneBlank(s ...string) bool

IsNoneBlank checks if none of the string are empty ("") or whitespace

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IsNoneBlank("", " "))
	fmt.Println(strings.IsNoneBlank("  ", " a "))
	fmt.Println(strings.IsNoneBlank(" \t\r\n ", " a "))
	fmt.Println(strings.IsNoneBlank("a", " a "))
	fmt.Println(strings.IsNoneBlank(" a ", " a "))
	fmt.Println(strings.IsNoneBlank(" \t\r\n a \t\r\n ", " a "))

}
Output:

false
false
false
true
true
true

func IsNotBlank

func IsNotBlank(s string) bool

IsNotBlank checks if a string is not empty ("") and not whitespace only.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.IsNotBlank(""))
	fmt.Println(strings.IsNotBlank("  "))
	fmt.Println(strings.IsNotBlank(" \t\r\n "))
	fmt.Println(strings.IsNotBlank("a"))
	fmt.Println(strings.IsNotBlank(" a  "))
	fmt.Println(strings.IsNotBlank(" \t\r\n a \t\r\n "))

}
Output:

false
false
false
true
true
true

func Join

func Join(a []string, sep string) string

Join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.

func JoinIgnoreBlank

func JoinIgnoreBlank(a []string, sep string) string

JoinIgnoreBlank join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.

func Left

func Left(s string, length int) string

Left gets the leftmost len characters of a String.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Left("abc", 2))
	fmt.Println(strings.Left("abc", 4))

}
Output:

ab
abc

func Map

func Map(mapping func(rune) rune, s string) string

Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.

func Mid

func Mid(s string, start, length int) string

Mid gets len characters from the middle of a String.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Mid("abc", 0, 2))
	fmt.Println(strings.Mid("abc", 0, 4))
	fmt.Println(strings.Mid("abc", -2, 2))

}
Output:

ab
abc
bc

func NewReader

func NewReader(s string) *strings.Reader

NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.

func NewReplacer

func NewReplacer(oldnew ...string) *strings.Replacer

NewReplacer returns a new Replacer from a list of old, new string pairs. Replacements are performed in order, without overlapping matches.

func Overlay

func Overlay(str, overlay string, start, end int) string

Overlay overlays part of a String with another String.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Overlay("", "abc", 0, 0))
	fmt.Println(strings.Overlay("abcdef", "", 2, 4))
	fmt.Println(strings.Overlay("abcdef", "", 4, 2))
	fmt.Println(strings.Overlay("abcdef", "zzzz", 2, 4))
	fmt.Println(strings.Overlay("abcdef", "zzzz", 4, 2))
	fmt.Println(strings.Overlay("abcdef", "zzzz", -1, 4))
	fmt.Println(strings.Overlay("abcdef", "zzzz", 2, 8))
	fmt.Println(strings.Overlay("abcdef", "zzzz", -2, -3))
	fmt.Println(strings.Overlay("abcdef", "zzzz", 8, 10))
	fmt.Println(strings.Overlay("世界hello", "zzzz", 2, 4))
}
Output:

abc
abef
abef
abzzzzef
abzzzzef
zzzzef
abzzzz
zzzzabcdef
abcdefzzzz
世界zzzzllo

func Pad

func Pad(in string, size int) string

Pad pads a string in a larger string of size using the space character (' ').

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%#q\n", strings.Pad("bat", 5))

}
Output:

` bat `

func PadEnd

func PadEnd(s string, size int) string

PadEnd right pad a String with spaces.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%#q\n", strings.PadEnd("bat", 5))

}
Output:

`bat  `

func PadLeft

func PadLeft(in string, size int, sep string) string

PadLeft left pad a String with a specified character.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.PadLeft("bat", 5, "*"))

}
Output:

**bat

func PadRight

func PadRight(in string, size int, sep string) string

PadRight right pad a String with a specified character.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.PadRight("bat", 5, "*"))

}
Output:

bat**

func PadStart

func PadStart(s string, size int) string

PadStart left pad a String with spaces.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%#q\n", strings.PadStart("bat", 5))

}
Output:

`  bat`

func Remove

func Remove(s, remove string) string

Remove removes all occurrences of a substring from within the source string.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Remove("abc", ""))
	fmt.Println(strings.Remove("queued", "ue"))
	fmt.Println(strings.Remove("queued", "zz"))

}
Output:

abc
qd
queued

func RemoveBlank

func RemoveBlank(s string) (d string)

RemoveBlank removes all whitespaces from a string.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.RemoveBlank(" a\nb   c\td e \rf   g    "))

}
Output:

ab cd e f g

func RemoveEnd

func RemoveEnd(s, remove string) string

RemoveEnd removes a substring only if it is at the end of a source string, otherwise returns the source string.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.RemoveEnd("abc", ""))
	fmt.Println(strings.RemoveEnd("www.domain.com", ".com"))
	fmt.Println(strings.RemoveEnd("www.domain.com", "domain"))

}
Output:

abc
www.domain
www.domain.com

func RemoveSpace

func RemoveSpace(s string) (d string)

RemoveSpace removes all whitespaces from a string.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.RemoveSpace(" a\nb   c\td e \rf   g    "))

}
Output:

abcdefg

func RemoveStart

func RemoveStart(s, remove string) string

RemoveStart removes a substring only if it is at the beginning of a source string, otherwise returns the source string.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.RemoveStart("abc", ""))
	fmt.Println(strings.RemoveStart("www.domain.com", "www."))
	fmt.Println(strings.RemoveStart("www.domain.com", "domain"))

}
Output:

abc
domain.com
www.domain.com

func Repeat

func Repeat(s string, count int) string

Repeat returns a new string consisting of count copies of the string s.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Repeat("-", 10))

}
Output:

----------

func Replace

func Replace(s, old, new string, n int) string

Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If n < 0, there is no limit on the number of replacements.

func Right(s string, length int) string

Right gets the rightmost len characters of a String.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Right("abc", 2))
	fmt.Println(strings.Right("abc", 4))

}
Output:

bc
abc

func Runes

func Runes(s string) []rune

Runes returns the []rune value represented by the string

func Slice

func Slice(s string, start, end int) string

Slice the returned substring starts with the character in the start position and ends before the end position.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.Slice("abc", 0, 2))
	fmt.Println(strings.Slice("abc", 2, 4))
	fmt.Println(strings.Slice("abc", -2, -1))
	fmt.Println(strings.Slice("abc", -7, -6))

}
Output:

ab
c
b

func Split

func Split(s, sep string) []string

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators. If sep is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%q\n", strings.Split("a,b,c", ","))
	fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
	fmt.Printf("%q\n", strings.Split(" xyz ", ""))
	fmt.Printf("%q\n", strings.Split(" x  y   z ", " "))
	fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
}
Output:

["a" "b" "c"]
["" "man " "plan " "canal panama"]
[" " "x" "y" "z" " "]
["" "x" "" "y" "" "" "z" ""]
[""]

func SplitAfter

func SplitAfter(s, sep string) []string

SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings. If sep is empty, SplitAfter splits after each UTF-8 sequence. It is equivalent to SplitAfterN with a count of -1.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
}
Output:

["a," "b," "c"]

func SplitAfterN

func SplitAfterN(s, sep string, n int) []string

SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings. If sep is empty, SplitAfterN splits after each UTF-8 sequence. The count determines the number of substrings to return:

n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substrings
Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
}
Output:

["a," "b,c"]

func SplitN

func SplitN(s, sep string, n int) []string

SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators. If sep is empty, SplitN splits after each UTF-8 sequence. The count determines the number of substrings to return:

n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substrings
Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
	z := strings.SplitN("a,b,c", ",", 0)
	fmt.Printf("%q (nil = %v)\n", z, z == nil)
}
Output:

["a" "b,c"]
[] (nil = true)

func Title

func Title(s string) string

Title returns a copy of the string s with all Unicode letters that begin words mapped to their title case.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"",
		"a",
		"cat",
		"cAt",
		"aaa aaa aaa",
		"Aaa Aaa Aaa",
		"123a456",
		"double-blind",
		"ÿøû"}
	for _, v := range s {
		fmt.Println(strings.Title(v))
	}

}
Output:


A
Cat
CAt
Aaa Aaa Aaa
Aaa Aaa Aaa
123a456
Double-Blind
Ÿøû

func ToLower

func ToLower(s string) string

ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"",
		"abc",
		"AbC123",
		"azAZ09_"}
	for _, v := range s {
		fmt.Println(strings.ToLower(v))
	}

}
Output:


abc
abc123
azaz09_

func ToLowerFirst

func ToLowerFirst(s string) string

ToLowerFirst returns a copy of the string s with first Unicode letters mapped to their lower case.

func ToLowerSpecial

func ToLowerSpecial(c unicode.SpecialCase, s string) string

ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their lower case, giving priority to the special casing rules.

func ToTitle

func ToTitle(s string) string

ToTitle returns a copy of the string s with all Unicode letters mapped to their title case.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"",
		"a",
		"cat",
		"cAt",
		"aaa aaa aaa",
		"Aaa Aaa Aaa",
		"123a456",
		"double-blind",
		"ÿøû"}
	for _, v := range s {
		fmt.Println(strings.ToTitle(v))
	}

}
Output:


A
CAT
CAT
AAA AAA AAA
AAA AAA AAA
123A456
DOUBLE-BLIND
ŸØÛ

func ToTitleSpecial

func ToTitleSpecial(c unicode.SpecialCase, s string) string

ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their title case, giving priority to the special casing rules.

func ToUpper

func ToUpper(s string) string

ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"",
		"abc",
		"AbC123",
		"azAZ09_"}
	for _, v := range s {
		fmt.Println(strings.ToUpper(v))
	}

}
Output:


ABC
ABC123
AZAZ09_

func ToUpperFirst

func ToUpperFirst(s string) string

ToUpperFirst returns a copy of the string s with first Unicode letters mapped to their upper case.

func ToUpperSpecial

func ToUpperSpecial(c unicode.SpecialCase, s string) string

ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their upper case, giving priority to the special casing rules.

func Trim

func Trim(s string, cutset string) string

Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("[%q]\n", strings.Trim("abba", ""))
	fmt.Printf("[%q]\n", strings.Trim(" !!! Achtung !!! ", "! "))
}
Output:

["abba"]
["Achtung"]

func TrimFunc

func TrimFunc(s string, f func(rune) bool) string

TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.

func TrimLeft

func TrimLeft(s string, cutset string) string

TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("[%q]\n", strings.TrimLeft("abba", ""))
	fmt.Printf("[%q]\n", strings.TrimLeft(" !!! Achtung !!! ", "! "))
}
Output:

["abba"]
["Achtung !!! "]

func TrimLeftFunc

func TrimLeftFunc(s string, f func(rune) bool) string

TrimLeftFunc returns a slice of the string s with all leading Unicode code points c satisfying f(c) removed.

func TrimPrefix

func TrimPrefix(s, prefix string) string

TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.

func TrimRight

func TrimRight(s string, cutset string) string

TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Printf("[%q]\n", strings.TrimRight("abba", ""))
	fmt.Printf("[%q]\n", strings.TrimRight(" !!! Achtung !!! ", "! "))
}
Output:

["abba"]
[" !!! Achtung"]

func TrimRightFunc

func TrimRightFunc(s string, f func(rune) bool) string

TrimRightFunc returns a slice of the string s with all trailing Unicode code points c satisfying f(c) removed.

func TrimSpace

func TrimSpace(s string) string

TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
}
Output:

a lone gopher

func TrimSpaceNQuote1

func TrimSpaceNQuote1(s string) string

TrimSpaceNQuote1 returns a slice of the string s, with all leading and trailing white space or quote removed, as defined by Unicode.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	fmt.Println(strings.TrimSpaceNQuote1("     "))
	fmt.Println(strings.TrimSpaceNQuote1(" `  `  "))
	fmt.Println(strings.TrimSpaceNQuote1(" '  '  "))
	fmt.Println(strings.TrimSpaceNQuote1("aaa"))
	fmt.Println(strings.TrimSpaceNQuote1("  aaa   "))
	fmt.Println(strings.TrimSpaceNQuote1("`aaa`"))
	fmt.Println(strings.TrimSpaceNQuote1("'aaa'"))
	fmt.Println(strings.TrimSpaceNQuote1("  ` aaa `  "))
	fmt.Println(strings.TrimSpaceNQuote1("  `' aaa '`  "))
	fmt.Println(strings.TrimSpaceNQuote1("  `  ' aaa '  `  "))
	fmt.Println(strings.TrimSpaceNQuote1(" \t\n '  a lone gopher   ' \n\t\r\n"))
	fmt.Println(strings.TrimSpaceNQuote1(" \t\n '`  a lone gopher  `' \n\t\r\n"))
}
Output:


aaa
aaa
aaa
aaa
aaa
' aaa '
' aaa '
a lone gopher
`  a lone gopher  `

func TrimSuffix

func TrimSuffix(s, suffix string) string

TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

func UnCamel

func UnCamel(s, sep string) string

UnCamel returns a copy of the string s with all Unicode letters that begin words mapped to their uncamel case.

Example
package main

import (
	"fmt"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

func main() {
	s := []string{"",
		"A",
		"Cat",
		"cAt",
		"AaaAaaAaa",
		" AaaAaaAaa ",
		"123a456",
		"DoubleBlind",
		"Ÿøû"}
	for _, v := range s {
		fmt.Println(strings.UnCamel(v, "_"))
	}

}
Output:


a
cat
c_at
aaa_aaa_aaa
aaa_aaa_aaa
123a456
double_blind
ÿøû

Types

This section is empty.

Jump to

Keyboard shortcuts

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