从字符串中取出随机字母
Take random letters out from a string
我想从字符串中删除 3 个随机字母。
我可以使用 substr()
或 slice()
之类的函数,但它不会让我取出随机字母。
这是我现在的演示。
如有任何帮助,我们将不胜感激!
你可以split
the string to an array, splice
random items, and join
返回一个字符串:
var arr = str.split('');
for(var i=0; i<3; ++i)
arr.splice(Math.floor(Math.random() * arr.length), 1);
str = arr.join('');
var str = "cat123",
amountLetters = 3,
randomString = "";
for(var i=0; i < amountLetters; i++) {
randomString += str.substr(Math.floor(Math.random()*str.length), 1);
}
alert(randomString);
fiddle:
http://jsfiddle.net/euuhyfr4/7/
var str = "hello world";
for(var i = 0; i < 3; i++) {
str = removeRandomLetter(str);
}
alert(str);
function removeRandomLetter(str) {
var pos = Math.floor(Math.random()*str.length);
return str.substring(0, pos)+str.substring(pos+1);
}
如果你想用其他随机字符替换3个随机字符,你可以使用这个函数3次:
function substitute(str) {
var pos = Math.floor(Math.random()*str.length);
return str.substring(0, pos) + getRandomLetter() + str.substring(pos+1);
}
function getRandomLetter() {
var letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var pos = Math.floor(Math.random()*letters.length);
return letters.charAt(pos);
}
您可以使用不带任何参数的拆分方法。
这会将 return 所有字符作为一个数组。
然后您可以使用 Generating random whole numbers in JavaScript in a specific range? 中描述的任何随机化器函数,然后使用该位置获取该位置的字符。
在这里看看@我的实现
var str = "cat123";
var strArray = str.split("");
function getRandomizer(bottom, top) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
alert("Total length " + strArray.length);
var nrand = getRandomizer(1, strArray.length);
alert("Randon number between range 1 - length of string " + nrand);
alert("Character @ random position " + strArray[nrand]);
This answer 表示
It is faster to slice the string twice [...] than using a split followed by a join [...]
因此,虽然 工作得很好,但我相信更快的实施是:
function removeRandom(str, amount)
{
for(var i = 0; i < amount; i++)
{
var max = str.length - 1;
var pos = Math.round(Math.random() * max);
str = str.slice(0, pos) + str.slice(pos + 1);
}
return str;
}
另见 this fiddle。
您可以随机排列字符串中的字符,然后删除前 3 个字符
var str = 'congratulations';
String.prototype.removeItems = function (num) {
var a = this.split(""),
n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("").substring(num);
}
alert(str.removeItems(3));
我想从字符串中删除 3 个随机字母。
我可以使用 substr()
或 slice()
之类的函数,但它不会让我取出随机字母。
这是我现在的演示。
如有任何帮助,我们将不胜感激!
你可以split
the string to an array, splice
random items, and join
返回一个字符串:
var arr = str.split('');
for(var i=0; i<3; ++i)
arr.splice(Math.floor(Math.random() * arr.length), 1);
str = arr.join('');
var str = "cat123",
amountLetters = 3,
randomString = "";
for(var i=0; i < amountLetters; i++) {
randomString += str.substr(Math.floor(Math.random()*str.length), 1);
}
alert(randomString);
fiddle: http://jsfiddle.net/euuhyfr4/7/
var str = "hello world";
for(var i = 0; i < 3; i++) {
str = removeRandomLetter(str);
}
alert(str);
function removeRandomLetter(str) {
var pos = Math.floor(Math.random()*str.length);
return str.substring(0, pos)+str.substring(pos+1);
}
如果你想用其他随机字符替换3个随机字符,你可以使用这个函数3次:
function substitute(str) {
var pos = Math.floor(Math.random()*str.length);
return str.substring(0, pos) + getRandomLetter() + str.substring(pos+1);
}
function getRandomLetter() {
var letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var pos = Math.floor(Math.random()*letters.length);
return letters.charAt(pos);
}
您可以使用不带任何参数的拆分方法。 这会将 return 所有字符作为一个数组。
然后您可以使用 Generating random whole numbers in JavaScript in a specific range? 中描述的任何随机化器函数,然后使用该位置获取该位置的字符。
在这里看看@我的实现
var str = "cat123";
var strArray = str.split("");
function getRandomizer(bottom, top) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
alert("Total length " + strArray.length);
var nrand = getRandomizer(1, strArray.length);
alert("Randon number between range 1 - length of string " + nrand);
alert("Character @ random position " + strArray[nrand]);
This answer 表示
It is faster to slice the string twice [...] than using a split followed by a join [...]
因此,虽然
function removeRandom(str, amount)
{
for(var i = 0; i < amount; i++)
{
var max = str.length - 1;
var pos = Math.round(Math.random() * max);
str = str.slice(0, pos) + str.slice(pos + 1);
}
return str;
}
另见 this fiddle。
您可以随机排列字符串中的字符,然后删除前 3 个字符
var str = 'congratulations';
String.prototype.removeItems = function (num) {
var a = this.split(""),
n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("").substring(num);
}
alert(str.removeItems(3));