feat: add HideString

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

No files matched your search

+27
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
}