stringsx

package module
v0.0.0-...-f59a4b8 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Copyright 2020 stringsx Author(https://github.com/yudeguang/stringsx). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/stringsx. 字符串处理包,对标准库字符串的补充

Copyright 2020 stringsx Author(https://github.com/yudeguang/stringsx). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/stringsx. 字符串处理包,对标准库字符串的补充

Copyright 2020 stringsx Author(https://github.com/yudeguang/stringsx). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/yudeguang/stringsx.

Index

Constants

View Source
const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
	GBK     = Charset("GBK")
	GB2312  = Charset("HZGB2312")
)

Variables

This section is empty.

Functions

func After

func After(s, sep string) string

返回第一次出现sep之后的字串符

func AfterLast

func AfterLast(s, sep string) string

返回最后一次出现sep之后的字符串

func AfterNSep

func AfterNSep(s, sep string, nTimes int) string

返回第N次出现sep之后的字符串

func Before

func Before(s, sep string) string

返回第一次出现sep之前的字符串

func BeforeLast

func BeforeLast(s, sep string) string

返回最后一次出现sep之前的字符串

func BeforeNSep

func BeforeNSep(s, sep string, nTimes int) string

返回第N次出现sep之前的字符串

func Between

func Between(s, begin, end string) string

返回第一次出现在两个字符串接之间的字符串

func CRC32

func CRC32(str string) uint32

生成crc32

func CenterPad

func CenterPad(s string, length int, pad string) string

返回把中间字符按一定规则替换后的字符串接 CenterPad("hello", 10, "*") => "he*****llo"

func CommaNumbersLettersLeft

func CommaNumbersLettersLeft(s string) string

只保留逗号以及数字和英文字母,因为逗号一般用于分隔文本

func ContainsHan

func ContainsHan(s string) bool

判断rune是否包含汉字

func FmtHTML

func FmtHTML(s string) string

HTML中,经常有换行符号,前后有空格等

func GB18030ToUTF8

func GB18030ToUTF8(s string) string

GB18030转换为UTF8,应优先使用GB18030,GB18030为GBK的超集,GBK又为GB2312的超集

func GB2312ToUTF8

func GB2312ToUTF8(s string) string

GB2312转换为utf8

func GBKToUTF8

func GBKToUTF8(s string) string

GBK转换为utf8

func HanLeft

func HanLeft(s string) string

只保留字符串中的汉字

func IsLetter

func IsLetter(s string) bool

判断字符串是否是由纯字母组成

func IsLetterLower

func IsLetterLower(b byte) bool

判断是否是小写字母

func IsLetterUpper

func IsLetterUpper(b byte) bool

判断是否是大写字母

func IsNumber

func IsNumber(s string) bool

判断字符串是否是由纯数字组成

func JoinInterface

func JoinInterface(sep string, elems ...interface{}) string

用分隔符sep把若干个字符或int,double等类型数据拼接在一起,实际为strings.Join的变体形式

func JoinInterface(sep string, args ...interface{}) string {
	l := len(args)
	switch l {
	case 0:
		return ""
	case 1:
		return fmt.Sprint(args[0])
	case 2:
		return fmt.Sprint(args[0]) + sep + fmt.Sprint(args[1])
	case 3:
		return fmt.Sprint(args[0]) + sep + fmt.Sprint(args[1]) + sep + fmt.Sprint(args[2])
	}
	var buffer bytes.Buffer
	//前面若干条中间要加sep
	for i := 0; i < l-1; i++ {
		buffer.WriteString(fmt.Sprint(args[i]) + sep)
	}
	//最后次不加sep
	buffer.WriteString(fmt.Sprint(args[l-1]))
	return buffer.String()
}

func JoinInts

func JoinInts(sep string, elems ...int) string

func JoinStrings

func JoinStrings(sep string, args ...string) string

用分隔符sep把若干个字符拼接在一起,实际为strings.Join的变体形式

func Left

func Left(s string, n int) string

返回左侧N个字符

func LeftPad

func LeftPad(s string, length int, pad string) string

返回字符串str,左面用字符串padstr填补直到str是len个字符长,,此函数与mysql中LPAD()行为保持一致 LeftPad("hello", 4, " ") => "hello" LeftPad("hello", 10, " ") => " hello" LeftPad("hello", 10, "123") => "12312hello"

func LeftRightPad

func LeftRightPad(s string, length int, pad string) string

返回两侧字符按一定规则替换后的字符串接 LeftRightPad("hello", 4, " ") => "hell" LeftRightPad("hello", 10, " ") => " hello " LeftRightPad("hello", 10, "123") => "12hello123"

func LettersLeft

func LettersLeft(s string) string

只保留英文字母

func MD5

func MD5(str string) string

生成md5

func MostSimilar

func MostSimilar(a string, b []string) string

返回最相似的一个字符串,前缀匹配优先 用于特定的项目 如果一点相似度都没有,则返回空

func MostSimilarWithScore

func MostSimilarWithScore(a string, b []string) *similarity.Match

返回相似度最高的那个字符串 并且带匹配度 此为通用算法

func MostSimilars

func MostSimilars(a string, b []string) []string

返回经过筛选后的若干条数据,按近似程度从高到低排序

func NumbersLeft

func NumbersLeft(s string) string

只保留阿拉伯数字

func NumbersLettersLeft

func NumbersLettersLeft(s string) string

只保留数字和英文字母,删除其它类型字母及标点符号

func Rand

func Rand(s string) string

返回随机打乱后的字符串

func RelevantCharactersLeft

func RelevantCharactersLeft(s, RelevantCharacter string) string

保留输入的相关字符

func RemoveHan

func RemoveHan(s string) string

移除字符串中的汉字

func Reverse

func Reverse(s string) string

返回倒序字符串

func Right(s string, n int) string

返回右侧N个字符

func Rightpad

func Rightpad(s string, length int, pad string) string

返回字符串str,右面用字符串padstr填补直到str是len个字符长,此函数与mysql中RPAD()行为保持一致 Rightpad("hello", 4, " ") => "hello" Rightpad("hello", 10, " ") => "hello " Rightpad("hello", 10, "123") => "hello12312"

func RuneIsHan

func RuneIsHan(r rune) bool

判断单个rune是否是汉字

func SHA1

func SHA1(str string) string

生成sha1

func SplitByLen

func SplitByLen(s string, sepLen int) []string

按固定的长度拆分字符串

func UTF8ToGB18030

func UTF8ToGB18030(s string) string

UTF8转GB18030,注意,打印出来一般是乱码

func UTF8ToGB2312

func UTF8ToGB2312(s string) string

UTF8转GB2312,注意,打印出来一般是乱码

func UTF8ToGBK

func UTF8ToGBK(s string) string

UTF8转GBK,注意,打印出来一般是乱码

Types

type Charset

type Charset string

Jump to

Keyboard shortcuts

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