繁体   English   中英

在 javascript 中搜索缺少字母的字符串

[英]Searching a string for missing letters of the alphabet in javascript

我正在编写一些代码,这些代码将搜索一个字符串并返回丢失的任何字母表中的字母。 这就是我所拥有的:

function findWhatsMissing(s){
    var a = "abcdefghijklmnopqrstuvwxyz";
    //remove special characters
    s.replace(/[^a-zA-Z]/g, "");
    s = s.toLowerCase();
    //array to hold search results
    var hits = [];

    //loop through each letter in string
    for (var i = 0; i < a.length; i++) {
        var j = 0;
        //if no matches are found, push to array
        if (a[i] !== s[j]) {
                hits.push(a[i]);
        }
        else {
            j++;
        }
    }
    //log array to console
    console.log(hits);
}

但是使用测试用例:findWhatsMissing("dabc");

将 d 之前的所有字母添加到缺失数组中。

任何帮助将不胜感激。

在循环中,您可以使用indexOf()来查看输入中是否存在该字母。 像这样的东西会起作用:

for (var i = 0; i < a.length; i++) {
    if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
}

希望有所帮助! 你可以看到它在这个JS小提琴中工作: https//jsfiddle.net/573jatx1/1/

正如Adam Konieska所说。 这样的东西会起作用:

   function findWhatsMissing(s) {
        var a = "abcdefghijklmnopqrstuvwxyz";
        s = s.toLowerCase();
        var hits = [];
        for (var i = 0; i < a.length; i++) {
            if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
        }

       console.log(hits);
    }

    findWhatsMissing("d a b c");

可以使用Array.prototype.filter()并使用indexOf()在每个循环检查字符串中

function findWhatsMissing(s){
    var a = "abcdefghijklmnopqrstuvwxyz";
    //remove special characters
    s = s.replace(/[^a-zA-Z]/g, "");
    s = s.toLowerCase();
    return a.split('').filter(function(letter){
      return s.indexOf(letter) === -1;
    });
}

alert( findWhatsMissing('d f v'))

你可以使用indexOf:

 function findWhatsMissing(s){ var a = "abcdefghijklmnopqrstuvwxyz"; //remove special characters s = s.replace(/[^a-zA-Z]/g, ""); s = s.toLowerCase(); //array to hold search results var hits = []; //loop through each letter in string for (var i = 0; i < a.length; i++) { //if no matches are found, push to array if (s.indexOf(a[i]) == -1) { hits.push(a[i]); } } //log array to console return hits; } alert(JSON.stringify(findWhatsMissing(' dabc '))); 

或两个for循环:

 function findWhatsMissing(s){ var a = "abcdefghijklmnopqrstuvwxyz"; //remove special characters s = s.replace(/[^a-zA-Z]/g, ""); s = s.toLowerCase(); //array to hold search results var hits = []; //loop through each letter in string for (var i = 0; i < a.length; i++) { //if no matches are found, push to array var found = false; for (var j = 0; j < s.length; j++) { if (s[j] == a[i]) { found = true; break; } } if (!found) { hits.push(a[i]); } } //log array to console return hits; } alert(JSON.stringify(findWhatsMissing(' dabc '))); 

function missingchar(str) {
let arr = new Array(26);
let mis_str = '';
Acharcode = 'A'.charCodeAt();
Zcharcode = 'Z'.charCodeAt();
for (i = Acharcode; i <= Zcharcode; i++) {
    arr[i] = String.fromCharCode(i).toLowerCase();
}
arr.map((item, index) => {
    if (str.indexOf(item) == -1) {
        mis_str += item;
    }
})
return mis_str;

}

console.log(missingchar('satya'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM