Skip to content

Commit

Permalink
feat: add str limit
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhizheng committed May 7, 2021
1 parent 9783a9f commit c2190a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pgo.Uniqid("") // 608a594ee0624
pgo.MbStrlen("中文 1") // 4
pgo.Explode(",","hello,world")
pgo.Strpos("+1s","s") // 2
// 将字符串以指定长度进行截断
pgo.StrLimit("测试2q文字超出,符号补充 1a",10,"...") // 测试2q文字超出,符...

pgo.Blank(0) // false
pgo.IsNumeric("000") // true
Expand Down
9 changes: 9 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ func Explode(delimiter, text string) []string {

func Strpos(haystack, needle string) int {
return strings.Index(haystack, needle)
}

func StrLimit(str string , LimitLength int, delimiter string) string {
strLen := MbStrlen(str)
if strLen > LimitLength {
str = string([]rune(str)[:LimitLength]) + delimiter
return str
}
return str
}
5 changes: 5 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ func TestExplode(t *testing.T) {
func TestStrpos(t *testing.T) {
strpos := Strpos("+1s","s")
assert.Equal(t, 2, strpos)
}

func TestStrLimit(t *testing.T) {
s := StrLimit("测试2q文字超出,符号补充 1a",10,"...")
assert.Equal(t, "测试2q文字超出,符...", s)
}

0 comments on commit c2190a7

Please sign in to comment.