David Barranco
David Barranco

Reputation: 452

golang - Replacing string characters by numbers golang

I'm trying to run this piece of code to replace a character of a string by a random number:

//Get the position between 0 and the length of the string-1  to insert a random number
    position := rand.Intn(count - 1)
    strings.Replace(RandomString,)

    fmt.Println("Going to change the position number: ", position)
    //Insert a random number between [0-9] in the position
    RandomString = RandomString[:position] + string(rand.Intn(9)) + RandomString[position+1:]

When this is executed, the output is:

I was going to generate..  ljFsrPaUvmxZFFw
Going to change the position number:  2
Random string:  lsrPaUvmxZFFw

Could anyone help me about inserting that number inside the desired string position? I didn't find any duplicate case of this.

Thanks in advance.

Upvotes: 0

Views: 548

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109406

There are no printable characters in the range [0, 9). If you want an ascii value, start at 48 ('0'). You probably want to include 9 too, so use rand.Intn(10)

string('0' + rand.Intn(10))

Upvotes: 3

Related Questions