stringx

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

stringx

Introduction

Extension/Helper of String Operation.

Features

  • Transform(Reverse, Rotate, Shuffle ...)
  • Construction(Pad, Repeat...)
  • Matching(IsAlpha, IsAlphanumeric, IsNumeric ...)

Documentation

Overview

Example (Pad)
fmt.Printf("PadLeft=[%s]\n", PadLeftSpace("abc", 7))
fmt.Printf("PadLeft=[%s]\n", PadLeftChar("abc", 7, '-'))
fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abc", 7, '-'))
fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abcd", 7, '-'))
Output:

PadLeft=[    abc]
PadLeft=[----abc]
PadCenter=[--abc--]
PadCenter=[-abcd--]
Example (Sub)
fmt.Printf("Sub-[0:100]=%s\n", Sub("", 0, 100))
fmt.Printf("Sub-facgbheidjk[3:9]=%s\n", Sub("facgbheidjk", 3, 9))
fmt.Printf("Sub-facgbheidjk[-50:100]=%s\n", Sub("facgbheidjk", -50, 100))
fmt.Printf("Sub-facgbheidjk[-3:length]=%s\n", Sub("facgbheidjk", -3, utf8.RuneCountInString("facgbheidjk")))
fmt.Printf("Sub-facgbheidjk[-3:-1]=%s\n", Sub("facgbheidjk", -3, -1))
fmt.Printf("Sub-zh英文hun排[2:5]=%s\n", Sub("zh英文hun排", 2, 5))
fmt.Printf("Sub-zh英文hun排[2:-1]=%s\n", Sub("zh英文hun排", 2, -1))
fmt.Printf("Sub-zh英文hun排[-100:-1]=%s\n", Sub("zh英文hun排", -100, -1))
fmt.Printf("Sub-zh英文hun排[-100:-90]=%s\n", Sub("zh英文hun排", -100, -90))
fmt.Printf("Sub-zh英文hun排[-10:-90]=%s\n", Sub("zh英文hun排", -10, -90))
Output:

Sub-[0:100]=
Sub-facgbheidjk[3:9]=gbheid
Sub-facgbheidjk[-50:100]=facgbheidjk
Sub-facgbheidjk[-3:length]=djk
Sub-facgbheidjk[-3:-1]=dj
Sub-zh英文hun排[2:5]=英文h
Sub-zh英文hun排[2:-1]=英文hun
Sub-zh英文hun排[-100:-1]=zh英文hun
Sub-zh英文hun排[-100:-90]=
Sub-zh英文hun排[-10:-90]=
Example (Substart)
fmt.Printf("SubStart-[0:]=%s\n", SubStart("", 0))
fmt.Printf("SubStart-[2:]=%s\n", SubStart("", 2))
fmt.Printf("SubStart-facgbheidjk[3:]=%s\n", SubStart("facgbheidjk", 3))
fmt.Printf("SubStart-facgbheidjk[-50:]=%s\n", SubStart("facgbheidjk", -50))
fmt.Printf("SubStart-facgbheidjk[-3:]=%s\n", SubStart("facgbheidjk", -3))
fmt.Printf("SubStart-zh英文hun排[3:]=%s\n", SubStart("zh英文hun排", 3))
Output:

SubStart-[0:]=
SubStart-[2:]=
SubStart-facgbheidjk[3:]=gbheidjk
SubStart-facgbheidjk[-50:]=facgbheidjk
SubStart-facgbheidjk[-3:]=djk
SubStart-zh英文hun排[3:]=文hun排

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDecodeRune = errors.New("error occurred on rune decoding")
)

Error pre define

Functions

func ContainsAnySubstrings

func ContainsAnySubstrings(s string, subs []string) bool

ContainsAnySubstrings returns whether s contains any of substring in slice.

func IsAlpha

func IsAlpha(s string) bool

IsAlpha checks if the string contains only unicode letters.

func IsAlphanumeric

func IsAlphanumeric(s string) bool

IsAlphanumeric checks if the string contains only Unicode letters or digits.

func IsNumeric

func IsNumeric(s string) bool

IsNumeric Checks if the string contains only digits. A decimal point is not a digit and returns false.

func MustReverse

func MustReverse(s string) string

MustReverse reverses a string, panics when error happens.

func PadCenterChar

func PadCenterChar(s string, size int, ch rune) string

PadCenterChar center pad a string with a specified character in a larger string(specified size). if the size is less than the param string, the param string is returned. note: size is unicode size.

func PadCenterSpace

func PadCenterSpace(s string, size int) string

PadCenterSpace center pad a string with space character(' ') in a larger string(specified size). if the size is less than the param string, the param string is returned. note: size is unicode size.

func PadLeftChar

func PadLeftChar(s string, size int, ch rune) string

PadLeftChar left pad a string with a specified character in a larger string (specified size). if the size is less than the param string, the param string is returned. note: size is unicode size.

func PadLeftSpace

func PadLeftSpace(s string, size int) string

PadLeftSpace left pad a string with space character(' ') in a larger string(specified size). if the size is less than the param string, the param string is returned. note: size is unicode size.

func PadRightChar

func PadRightChar(s string, size int, ch rune) string

PadRightChar right pad a string with a specified character in a larger string(specified size). if the size is less than the param string, the param string is returned. note: size is unicode size.

func PadRightSpace

func PadRightSpace(s string, size int) string

PadRightSpace right pad a string with space character(' ') in a large string(specified size). if the size is less than the param string, the param string is returned. note: size is unicode size.

func RemoveChar

func RemoveChar(s string, rmVal rune) string

RemoveChar removes all occurrences of a specified character from the string.

func RemoveString

func RemoveString(s, rmStr string) string

RemoveString removes all occurrences of a substring from the string.

func RepeatChar

func RepeatChar(ch rune, repeat int) string

RepeatChar returns padding using the specified delimiter repeated to a given length.

func Reverse

func Reverse(s string) (string, error)

Reverse reverses a string with error status returned.

func Rotate

func Rotate(s string, shift int) string

Rotate rotates(circular shift) a string of shift characters.

func Shuffle

func Shuffle(s string) string

Shuffle shuffles runes in a string and returns.

func Sub

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

Sub returns substring from specified string avoiding panics with index start and end. start, end are based on unicode(utf8) count.

func SubStart

func SubStart(s string, start int) string

SubStart returns substring from specified string avoiding panics with start. start, end are based on unicode(utf8) count.

Types

This section is empty.

Jump to

Keyboard shortcuts

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