gstr

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gstr provides functions for string handling.

字符串处理.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSlashes

func AddSlashes(str string) string

Quote string with slashes.

转义字符串中的单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

func Chr

func Chr(ascii int) string

Generate a single-byte string from a number.

返回相对应于 ascii 所指定的单个字符。

func ChunkSplit

func ChunkSplit(body string, chunkLen int, end string) string

Split a string into smaller chunks. Can be used to split a string into smaller chunks which is useful for e.g. converting BASE64 string output to match RFC 2045 semantics. It inserts end every chunkLen characters.

将字符串分割成小块。使用此函数将字符串分割成小块非常有用。 例如将BASE64的输出转换成符合RFC2045语义的字符串。 它会在每 chunkLen 个字符后边插入 end。

func Compare

func Compare(a, b string) int

Compare returns an integer comparing two strings lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

比较两个字符串。

func Contains

func Contains(str, substr string) bool

Contains reports whether substr is within str.

判断是否substr存在于str中。

func ContainsAny

func ContainsAny(s, chars string) bool

ContainsAny reports whether any Unicode code points in chars are within s.

判断是否s中是否包含chars指定的任意字符。

func ContainsI

func ContainsI(str, substr string) bool

Contains reports whether substr is within str, case-insensitive.

判断是否substr存在于str中(不区分大小写)。

func CountChars

func CountChars(str string, noSpace ...bool) map[string]int

Return information about words used in a string.

返回字符串中字符的使用情况。

func CountWords

func CountWords(str string) map[string]int

Return information about words used in a string.

返回字符串中单词的使用情况。

func Equal

func Equal(a, b string) bool

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

比较两个字符串是否相等(不区分大小写)。

func Explode

func Explode(delimiter, str string) []string

Split a string by a string, to an array. See http://php.net/manual/en/function.explode.php.

此函数返回由字符串组成的数组,每个元素都是 str 的一个子串,它们被字符串 delimiter 作为边界点分割出来。

func Fields

func Fields(str string) []string

Return the words used in a string.

分割字符串中的单词。

func HideStr

func HideStr(str string, percent int, hide string) string

HideStr replaces part of the the string by percentage from the middle.

按照百分比从字符串中间向两边隐藏字符(主要用于姓名、手机号、邮箱地址、身份证号等的隐藏),支持utf-8中文,支持email格式。

func Implode

func Implode(glue string, pieces []string) string

Join array elements with a string. http://php.net/manual/en/function.implode.php

用glue将字符串数组pieces连接为一个字符串。

func InArray

func InArray(a []string, s string) bool

InArray tests whether the given string s is in string array a.

判断字符串是否在数组中

func IsLetterLower

func IsLetterLower(b byte) bool

IsLetterLower tests whether the given byte b is in lower case.

判断给定字符是否小写

func IsLetterUpper

func IsLetterUpper(b byte) bool

IsLetterUpper tests whether the given byte b is in upper case.

判断给定字符是否大写

func IsNumeric

func IsNumeric(s string) bool

IsNumeric tests whether the given string s is numeric.

判断锁给字符串是否为数字.

func Join

func Join(array []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.

用sep将字符串数组array连接为一个字符串。

func LcFirst

func LcFirst(s string) string

LcFirst returns a copy of the string s with the first letter mapped to its lower case.

字符串首字母转换为小写

func Levenshtein

func Levenshtein(str1, str2 string, costIns, costRep, costDel int) int

Calculate Levenshtein distance between two strings. costIns: Defines the cost of insertion. costRep: Defines the cost of replacement. costDel: Defines the cost of deletion. See http://php.net/manual/en/function.levenshtein.php.

计算两个字符串之间的编辑距离(Levenshtein distance)。

func Nl2Br

func Nl2Br(str string, isXhtml ...bool) string

Inserts HTML line breaks before all newlines in a string. \n\r, \r\n, \r, \n

在字符串 string 所有新行之前插入 '<br />' 或 '<br>',并返回。

func NumberFormat

func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string

Format a number with grouped thousands. decimals: Sets the number of decimal points. decPoint: Sets the separator for the decimal point. thousandsSep: Sets the thousands separator. See http://php.net/manual/en/function.number-format.php.

以千位分隔符方式格式化一个数字.

func Ord

func Ord(char string) int

Convert the first byte of a string to a value between 0 and 255.

解析 char 二进制值第一个字节为 0 到 255 范围的无符号整型类型。

func Parse

func Parse(encodedString string, result map[string]interface{}) error

Parses the string into variables. f1=m&f2=n -> map[f1:m f2:n] f[a]=m&f[b]=n -> map[f:map[a:m b:n]] f[a][a]=m&f[a][b]=n -> map[f:map[a:map[a:m b:n]]] f[]=m&f[]=n -> map[f:[m n]] f[a][]=m&f[a][]=n -> map[f:map[a:[m n]]] f[][]=m&f[][]=n -> map[f:[map[]]] // Currently does not support nested slice. f=m&f[a]=n -> error // This is not the same as PHP. a .[[b=c -> map[a___[b:c]

将字符串解析成Map。

func Pos

func Pos(haystack, needle string, startOffset ...int) int

Find the position of the first occurrence of a substring in a string.

返回 needle 在 haystack 中首次出现的数字位置,找不到返回-1。

func PosI

func PosI(haystack, needle string, startOffset ...int) int

Find the position of the first occurrence of a case-insensitive substring in a string.

返回在字符串 haystack 中 needle 首次出现的数字位置(不区分大小写),找不到返回-1。

func PosR

func PosR(haystack, needle string, startOffset ...int) int

Find the position of the last occurrence of a substring in a string.

查找指定字符串在目标字符串中最后一次出现的位置,找不到返回-1。

func PosRI

func PosRI(haystack, needle string, startOffset ...int) int

Find the position of the last occurrence of a case-insensitive substring in a string.

以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位置,找不到返回-1。

func QuoteMeta

func QuoteMeta(str string) string

Returns a version of str with a backslash character (\) before every character that is among: .\+*?[^]($)

转义字符串,转义的特殊字符包括:.\+*?[^]($)。

func Repeat

func Repeat(input string, multiplier int) string

Repeat returns a new string consisting of multiplier copies of the string input.

按照指定大小创建重复的字符串。

func Replace

func Replace(origin, search, replace string, count ...int) string

Replace returns a copy of the string <origin> with string <search> replaced by <replace>.

字符串替换(大小写敏感)

func ReplaceByMap

func ReplaceByMap(origin string, replaces map[string]string) string

Replace string by map.

使用map进行字符串替换(大小写敏感)

func Reverse

func Reverse(str string) string

Reverse a string.

字符串反转.

func RuneLen

func RuneLen(str string) int

Get string length of unicode.

UTF-8字符串长度。

func SearchArray

func SearchArray(a []string, s string) int

Traverse the array to find the string index position, if not exist, return-1.

遍历数组查找字符串索引位置,如果不存在则返回-1,使用完整遍历查找.

func Shuffle

func Shuffle(str string) string

Randomly shuffles a string.

将字符串打乱。

func SimilarText

func SimilarText(first, second string, percent *float64) int

Calculate the similarity between two strings. See http://php.net/manual/en/function.similar-text.php.

计算两个字符串的相似度。

func Soundex

func Soundex(str string) string

Calculate the soundex key of a string. See http://php.net/manual/en/function.soundex.php.

计算字符串的SOUNDEX值,SOUNDEX为由四个字符组成的代码以评估两个字符串的相似性。

func Split

func Split(str, delimiter string) []string

Split a string by a string, to an array.

此函数返回由字符串组成的数组,每个元素都是 str 的一个子串,它们被字符串 delimiter 作为边界点分割出来。

func Str

func Str(haystack string, needle string) string

Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack. See http://php.net/manual/en/function.strstr.php.

查找字符串的首次出现。返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。

func StrLimit

func StrLimit(str string, length int, suffix ...string) string

Returns the portion of string specified by the <length> parameters, if the length of str is greater than <length>, then the <suffix> will be appended to the result.

字符串长度截取限制,超过长度限制被截取并在字符串末尾追加指定的内容,支持中文

func StripSlashes

func StripSlashes(str string) string

Un-quotes a quoted string.

反转义字符串。

func SubStr

func SubStr(str string, start int, length ...int) (substr string)

Returns the portion of string specified by the start and length parameters.

字符串截取,支持中文

func ToLower

func ToLower(s string) string

ToLower returns a copy of the string s with all Unicode letters mapped to their lower case. 字符串转换为小写

func ToUpper

func ToUpper(s string) string

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

字符串转换为大写

func Trim

func Trim(str string, characterMask ...string) string

Strip whitespace (or other characters) from the beginning and end of a string.

去除字符串首尾处的空白字符(或者其他字符)。

func TrimLeft

func TrimLeft(str string, characterMask ...string) string

Strip whitespace (or other characters) from the beginning of a string.

去除字符串首的空白字符(或者其他字符)。

func TrimLeftStr

func TrimLeftStr(str string, cut string) string

Strip all of the given <cut> string from the beginning of a string.

去除字符串首的给定字符串。

func TrimRight

func TrimRight(str string, characterMask ...string) string

Strip whitespace (or other characters) from the end of a string.

去除字符串尾的空白字符(或者其他字符)。

func TrimRightStr

func TrimRightStr(str string, cut string) string

Strip all of the given <cut> string from the end of a string.

去除字符串尾的给定字符串。

func UcFirst

func UcFirst(s string) string

UcFirst returns a copy of the string s with the first letter mapped to its upper case.

字符串首字母转换为大写

func UcWords

func UcWords(str string) string

Uppercase the first character of each word in a string.

大写字符串中每个单词的第一个字符。

func WordWrap

func WordWrap(str string, width int, br string) string

Wraps a string to a given number of characters. TODO: Enable cut param, see http://php.net/manual/en/function.wordwrap.php.

使用字符串断点将字符串打断为指定数量的字串。

Types

This section is empty.

Jump to

Keyboard shortcuts

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