gpt4 book ai didi

javascript - 如何在每个随机生成的字符串 Javascript 中添加公共(public)字符串

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:13 25 4
gpt4 key购买 nike

我正在使用 node.js 中的以下函数生成随机字符串。我想知道是否有任何方法可以在每个随机生成的字符串中使用公共(public)字符串适本地创建文本字符串。

编辑:公共(public)字符串可以位于生成字符串的任何位置

例如:

随机生成的字符串 - Cxqtooxyy4

我可以在该字符串中添加 'abc''ABC',如下所示 - 分别为 Cxqtoabcoxyy4CxqtoABCoxyy4

我的代码 -

var randomTextArrayGeneration = function(size)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i=0;i<size;i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}

谁能告诉我该怎么做?任何帮助都非常有帮助。

最佳答案

var n = text.length; //The size of your random string
var randomPosition = Math.floor((Math.random() * n) + 1); //Generate a random number between 1 and the size of your string

//Separate your string in 2 strings
var text1 = text.substring(1, randomPosition);
var text2 = text.substring(randomPosition, n);

//Create your final string by adding the common string between your two halves
var textFinal = text1 + commonString + text2;

return textFinal;

我不记得.substring()具体是如何工作的,您可能想在某些地方将1改为0。

关于javascript - 如何在每个随机生成的字符串 Javascript 中添加公共(public)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com