1

I want to replace string except first and last alphabet.

For example:

handsome -> h******e

한국어    -> 한*어

This is my code:

var final = string([]rune(username)[:1]
for i :=0l i <len([]rune(username)); i++{
 if i >1 {
  final = final + "*"
 }
}

1 Answer 1

3

If you convert the string to []rune, you can modify that slice and convert it back to string in the end:

func blur(s string) string {
    rs := []rune(s)
    for i := 1; i < len(rs)-1; i++ {
        rs[i] = '*'
    }
    return string(rs)
}

Testing it:

fmt.Println(blur("handsome"))
fmt.Println(blur("한국어"))

Output (try it on the Go Playground):

h******e
한*어

Note that this blur() function works with strings that have less than 3 characters too, in which case nothing will be blurred.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.