strutil

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 2 Imported by: 139

Documentation

Overview

Package strutil provides methods for working with strings

Index

Examples

Constants

This section is empty.

Variables

View Source
var EllipsisSuffix = "..."

EllipsisSuffix is ellipsis suffix

Functions

func After

func After(s, substr string) string

After returns part of the string after given substring

Example
fmt.Println(After("john@domain.com", "@"))
Output:

domain.com

func Before

func Before(s, substr string) string

Before returns part of the string before given substring

Example
fmt.Println(Before("john@domain.com", "@"))
Output:

john

func Concat

func Concat(s ...string) string

Concat is method for fast string concatenation

Example
fmt.Println(Concat("abc", " ", "123", " ", "ABC"))
Output:

abc 123 ABC

func Copy

func Copy(v string) string

Copy is method for force string copying

Example
fmt.Println(Copy("abc"))
Output:

abc

func Ellipsis

func Ellipsis(s string, maxSize int) string

Ellipsis trims given string (unicode supported)

Example
fmt.Println(Ellipsis("This is too long message to show", 18))
Output:

This is too lon...

func Exclude

func Exclude(data, substr string) string

Exclude excludes substring from given string

Example
fmt.Println(Exclude("This is funny message", " funny"))
Output:

This is message

func Extract

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

Extract extracts a substring safely (unicode NOT supported)

Example
fmt.Println(Extract("This is funny message", 8, 13))
Output:

funny

func Fields

func Fields(data string) []string

Fields splits the string data around each instance of one or more consecutive white space or comma characters

Example
fmt.Printf("%#v\n", Fields("Bob  Alice, 'Mary Key', \"John Dow\""))
Output:

[]string{"Bob", "Alice", "Mary Key", "John Dow"}

func HasPrefixAny

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

HasPrefixAny tests whether the string s begins with one of given prefixes

Example
fmt.Println(HasPrefixAny("www.domain.com", "dl", "www"))
fmt.Println(HasPrefixAny("api.domain.com", "dl", "www"))
Output:

true
false

func HasSuffixAny

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

HasSuffixAny tests whether the string s ends with one of given suffixes

Example
fmt.Println(HasSuffixAny("www.domain.com", ".com", ".org", ".net"))
fmt.Println(HasSuffixAny("www.domain.info", ".com", ".org", ".net"))
Output:

true
false
func Head(s string, n int) string

Head returns n first symbols from given string (unicode supported)

Example
fmt.Println(Head("This is funny message", 7))
Output:

This is

func Len

func Len(s string) int

Len returns number of symbols in string (unicode supported)

Example
fmt.Println(Len("Пример 例子 例 მაგალითად"))
Output:

21

func PrefixSize

func PrefixSize(str string, prefix rune) int

PrefixSize returns prefix size

Example
fmt.Println(PrefixSize("#### Header 4", '#'))
Output:

4

func Q

func Q(v ...string) string

Q is simple helper for working with default values

Example
var defaultValue = "john"
var user = ""

fmt.Println(Q(user, defaultValue))
Output:

john

func ReadField

func ReadField(data string, index int, multiSep bool, separators ...string) string

ReadField reads field with given index from data

Example
fmt.Println(ReadField("Bob    Alice\tJohn Mary", 2, true, " ", "\t"))
fmt.Println(ReadField("Bob:::Mary:John:", 3, false, ":"))
Output:

John
Mary

func ReplaceAll

func ReplaceAll(source, from, to string) string

ReplaceAll replaces all symbols in given string

Example
fmt.Println(ReplaceAll("Message", "es", "?"))
Output:

M???ag?

func Substr

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

Substr returns the part of a string between the start index and a number of characters after it (unicode supported)

Example
fmt.Println(Substr("Пример 例子 例 მაგალითად", 7, 2))
Output:

例子

func Substring

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

Substring returns the part of the string between the start and end (unicode supported)

Example
fmt.Println(Substring("Пример 例子 例 მაგალითად", 7, 9))
Output:

例子

func SuffixSize

func SuffixSize(str string, suffix rune) int

SuffixSize returns suffix size

Example
fmt.Println(SuffixSize("Message    ", ' '))
Output:

4

func Tail

func Tail(s string, n int) string

Tail returns n last symbols from given string (unicode supported)

Example
fmt.Println(Tail("This is funny message", 7))
Output:

message

Types

This section is empty.

Jump to

Keyboard shortcuts

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