javascript - Take random letters out from a string - Stack Overflow

I want to remove 3 RANDOM letters from a string.I can use something like substr() or slice() function b

I want to remove 3 RANDOM letters from a string.

I can use something like substr() or slice() function but it won't let me take the random letters out.

Here is the demo of what I have right now.

/

Any help would be appreciated!

I want to remove 3 RANDOM letters from a string.

I can use something like substr() or slice() function but it won't let me take the random letters out.

Here is the demo of what I have right now.

http://jsfiddle/euuhyfr4/

Any help would be appreciated!

Share Improve this question asked Apr 26, 2015 at 15:22 user4756836user4756836 1,3374 gold badges21 silver badges48 bronze badges 1
  • Remove a random 3 letter sequence from the string? such as abc'123' or a'bc1'23 where the '' denotes whats removed? – gsp8181 Commented Apr 26, 2015 at 15:38
Add a ment  | 

6 Answers 6

Reset to default 3
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);
}

If you want to replace 3 random charc with other random chars, you can use 3 times this function:

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); 
}

You can split the string to an array, splice random items, and join back to a string:

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/euuhyfr4/7/

This answer states that

It is faster to slice the string twice [...] than using a split followed by a join [...]

Therefore, while Oriol's answer works perfectly fine, I believe a faster implementation would be:

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;
}

See also this fiddle.

you can shuffle characters in your string then remove first 3 characters

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));

You can use split method without any args. This would return all chars as a array.

Then you can use any randomiser function as described in Generating random whole numbers in JavaScript in a specific range? , then use that position to get the character at that position.

Have a look @ my implementation here

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]);

Code @ here https://jsfiddle/1ryjedq6/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745213304a4616971.html

相关推荐

  • javascript - Take random letters out from a string - Stack Overflow

    I want to remove 3 RANDOM letters from a string.I can use something like substr() or slice() function b

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信