开发者

ActionScript: Insert text in string at random places?

开发者_如何学编程I'm building an ActionScript program in which I need to insert text into another string at random positions.

I have the text which strings will be inserted into; and I have the strings which will be inserted as an array.

However, I don't know how to go about inserting the elements of this array into the other string at random positions.

Any help will be highly appreciated.


The answer to your modified question:

var stringsToInsert:Array = ["abc", "def", "ghi"];
var text:String = "here is some text"

var textArr:Array = text.split(" ");

while(stringsToInsert.length)
{
    var randomPosition:uint = Math.round( Math.random() * textArr.length );
    textArr.splice(randomPosition, 0, stringsToInsert.pop());
}
text = textArr.join(" ");
trace(text);


while(arrayOftringsToInsert.length)
{
  var randomPosition:uint = Math.round( Math.random() * text.length )
  text = text.slice(0, randomPosition) + arrayOftringsToInsert.pop() + text.slice(randomPosition + 1, text.length)
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜