简体   繁体   English

golang-用数字代替字符串

[英]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) . [0, 9)范围内没有可打印的字符。 If you want an ascii value, start at 48 ( '0' ). 如果要一个ascii值,请从48( '0' )开始。 You probably want to include 9 too, so use rand.Intn(10) 您可能也想包含9,因此请使用rand.Intn(10)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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