Documentation ¶
Overview ¶
Package strutil provides methods for working with strings
Index ¶
- Variables
- func After(s, substr string) string
- func B(cond bool, positive, negative string) string
- func Before(s, substr string) string
- func Concat(s ...string) string
- func Copy(v string) string
- func Ellipsis(s string, maxSize int) string
- func Exclude(data, substr string) string
- func Extract(s string, start, end int) string
- func Fields(data string) []string
- func HasPrefixAny(s string, prefix ...string) bool
- func HasSuffixAny(s string, suffix ...string) bool
- func Head(s string, n int) string
- func IndexByteSkip(s string, c byte, skip int) int
- func Len(s string) int
- func LenVisual(s string) int
- func PrefixSize(str string, prefix rune) int
- func Q(v ...string) string
- func ReadField(data string, index int, multiSep bool, separators ...rune) string
- func ReplaceAll(source, replset, to string) string
- func ReplaceIgnoreCase(source, from, to string) string
- func SqueezeRepeats(s string, set string) string
- func Substr(s string, start, length int) string
- func Substring(s string, start, end int) string
- func SuffixSize(str string, suffix rune) int
- func Tail(s string, n int) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var EllipsisSuffix = "..."
EllipsisSuffix is ellipsis suffix
Functions ¶
func After ¶
After returns part of the string after given substring
Example ¶
fmt.Println(After("john@domain.com", "@"))
Output: domain.com
func B ¶
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 ¶
Before returns part of the string before given substring
Example ¶
fmt.Println(Before("john@domain.com", "@"))
Output: john
func Concat ¶
Concat is method for fast string concatenation
Example ¶
fmt.Println(Concat("abc", " ", "123", " ", "ABC"))
Output: abc 123 ABC
func Ellipsis ¶
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 ¶
Exclude excludes substring from given string
Example ¶
fmt.Println(Exclude("This is funny message", " funny"))
Output: This is message
func Extract ¶
Extract extracts a substring safely (unicode NOT supported)
Example ¶
fmt.Println(Extract("This is funny message", 8, 13))
Output: funny
func Fields ¶
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 \"Big John\" Dow"`))
Output: []string{"Bob", "Alice", "Mary Key", "John \"Big John\" Dow"}
func HasPrefixAny ¶
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 ¶
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 ¶
Head returns n first symbols from given string (unicode supported)
Example ¶
fmt.Println(Head("This is funny message", 7))
Output: This is
func IndexByteSkip ¶
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 ¶
Len returns number of symbols in string (unicode supported)
Example ¶
fmt.Println(Len("Пример 例子 例 მაგალითად")) fmt.Println(Len("😚😘🥰"))
Output: 21 3
func LenVisual ¶
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 ¶
PrefixSize returns prefix size
Example ¶
fmt.Println(PrefixSize("#### Header 4", '#'))
Output: 4
func Q ¶
Q is simple helper for working with default values
Example ¶
defaultValue := "john" user := "" fmt.Println(Q(user, defaultValue))
Output: john
func ReadField ¶
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 ¶
ReplaceAll replaces all symbols from replset in string
Example ¶
fmt.Println(ReplaceAll("Message", "es", "?"))
Output: M???ag?
func ReplaceIgnoreCase ¶
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 SqueezeRepeats ¶ added in v13.4.0
SqueezeRepeats replaces each sequence of a repeated character that is listed in the specified set
Example ¶
s := `"john"" "bob" """mary""` fmt.Println(SqueezeRepeats(s, `" `))
Output: "john" "bob" "mary"
func Substr ¶
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 ¶
Substring returns the part of the string between the start and end (unicode supported)
Example ¶
fmt.Println(Substring("Пример 例子 例 მაგალითად", 7, 9))
Output: 例子
func SuffixSize ¶
SuffixSize returns suffix size
Example ¶
fmt.Println(SuffixSize("Message ", ' '))
Output: 4
Types ¶
This section is empty.