golang - 用数字替换字符串字符golang

时间:2017-05-23 18:14:45

标签: string go random

我试图运行这段代码用随机数替换字符串的字符:

//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:]

执行此操作时,输出为:

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

有人可以帮我把这个数字插入所需的字符串位置吗?我没有找到任何重复的案例。

提前致谢。

1 个答案:

答案 0 :(得分:3)

[0, 9)范围内没有可打印的字符。如果您想要ascii值,请从48('0')开始。您可能也想要包含9,因此请使用rand.Intn(10)

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