简体   繁体   中英

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM