1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add HideString

This commit is contained in:
dudaodong
2023-05-23 18:39:59 +08:00
parent b1fcfce188
commit e149ae2f72
5 changed files with 146 additions and 1 deletions

View File

@@ -501,3 +501,30 @@ func Trim(str string, characterMask ...string) string {
return strings.Trim(str, trimChars)
}
// HideString hide some chars in source string with param `replaceChar`.
// replace range is origin[start : end]. [start, end)
// Play: todo
func HideString(origin string, start, end int, replaceChar string) string {
size := len(origin)
if start > size-1 || start < 0 || end < 0 || start > end {
return origin
}
if end > size {
end = size
}
if replaceChar == "" {
return origin
}
startStr := origin[0:start]
endStr := origin[end:size]
replaceSize := end - start
replaceStr := strings.Repeat(replaceChar, replaceSize)
return startStr + replaceStr + endStr
}