strutil

package
v12.120.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 2 Imported by: 44

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 B added in v12.53.0

func B(cond bool, positive, negative string) string

B is shorthand for choosing value by condition

Example
isAdmin := true
user := "bob"

fmt.Printf(
	B(isAdmin, "User %s is admin\n", "User %s isn't admin\n"),
	user,
)
Output:

User bob is admin

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 IndexByteSkip added in v12.105.0

func IndexByteSkip(s string, c byte, skip int) int

IndexByteSkip returns the index of the given byte in the string after skipping the first or the last N occurrences

Example
// Index from left
fmt.Println(IndexByteSkip("/home/john/projects/test.log", '/', 2))

// Index from right
fmt.Println(IndexByteSkip("/home/john/projects/test.log", '/', -1))
Output:

10
10

func Len

func Len(s string) int

Len returns number of symbols in string (unicode supported)

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

21
3

func LenVisual added in v12.83.0

func LenVisual(s string) int

LenVisual returns number of space required for rendering given string using monospaced font.

Warning: This method can be inaccurate in some cases, use with care

Example
k := "🥰 Пример 例子 例 მაგალითად"
l := LenVisual(k)

fmt.Println(k)
fmt.Println(strings.Repeat("^", l))
Output:

🥰 Пример 例子 例 მაგალითად
^^^^^^^^^^^^^^^^^^^^^^^^^^^

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
defaultValue := "john"
user := ""

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

john

func ReadField

func ReadField(data string, index int, multiSep bool, separators ...rune) 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, replset, to string) string

ReplaceAll replaces all symbols from replset in string

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

M???ag?

func ReplaceIgnoreCase added in v12.81.0

func ReplaceIgnoreCase(source, from, to string) string

ReplaceIgnoreCase replaces part of the string ignoring case

Example
fmt.Println(ReplaceIgnoreCase(
	"User bob has no item. Add items to user Bob?", "bob", "[Bob]",
))
Output:

User [Bob] has no item. Add items to user [Bob]?

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