0
我有一個字符串:The quick brown fox jumped over the lazy dogs.
我想插入一個,
(逗號和空格)到當前只有一個(空格)的隨機地點。我怎樣才能做到這一點?如何使用JavaScript將字符添加到字符串的隨機空間?
我有一個字符串:The quick brown fox jumped over the lazy dogs.
我想插入一個,
(逗號和空格)到當前只有一個(空格)的隨機地點。我怎樣才能做到這一點?如何使用JavaScript將字符添加到字符串的隨機空間?
另一種方式:
var string = 'The quick brown fox jumped over the lazy dogs.',
array = string.split(' '),
i = Math.random() * (array.length - 1) | 0;
array[i] += ',';
string = array.join(' ');
document.write(string);