直接上代码,这是比较挫的实现方法,不知各位大神有没有其它方法分享下
```
package main
import (
"fmt"
"strings"
)
func main() {
var phone string
phone = "13412345678"
old := ""
for k, v := range phone{
if k >=3 && k <=6{
old = old + string(v)
}
}
phone = strings.Replace(phone,old,"****",-1)
fmt.Println(phone) //134****5678
}
```
`func FormatText(str string) string {
if len(str) <= 10 {
return str
}
return str[:3] + "**" + str[len(str)-4:]
}`
#2
更多评论
```
package main
import (
"fmt"
"strings"
)
func main() {
phone := "13412345678"
slice := strings.Split(phone, "")
str := strings.Join(slice[0:3], "") + "****" + strings.Join(slice[7:], "")
fmt.Println(str)
}
```
#1
```
func FormatText(str string) string {
if len(str) <= 10 {
return str
}
return str[:3] + "**" + str[len(str)-4:]
}
```
#3