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

标签 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

谁能帮我把那个数字插入到所需的字符串位置?我没有找到任何重复的案例。

提前致谢。

最佳答案

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

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

关于string - golang - 用数字替换字符串字符 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44142182/

相关文章:

c - 一个用 C 语言确定字符串回文串最少插入次数的程序。我得到的错误是标准输出丢失

go - 使用Go从Firestore获取单个文档的惯用方式是什么?

golang 发出并发请求并合并响应

php - 如何使用 PHP 随机化图像数组

python - 生成 numpy 数组或随机分布的 0 和 1 的最快方法

javascript - 如何在javascript中将String变量转换为int?

javascript - 递归地分割包含html元素的字符串并在javascript中获取特定值

string - 你怎么能在低级别对字符串单词进行排序?

json - 在 Hugo 中用字符串按键查找数据

memory - 在Ubuntu中练习缓冲区溢出攻击