我正在尝试运行这段代码,用随机数替换字符串中的一个字符:
//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
谁能帮我把那个数字插入到所需的字符串位置?我没有找到任何重复的案例。
提前致谢。
最佳答案
[0, 9)
范围内没有可打印的字符。如果您想要一个 ascii 值,请从 48 ('0'
) 开始。您可能也想包括 9,所以使用 rand.Intn(10)
string('0' + rand.Intn(10))
关于string - golang - 用数字替换字符串字符 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44142182/