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

Searching a string for missing letters of the alphabet in javascript

本文关键字:javascript 字符串 搜索      更新时间:2023-09-26

我正在研究一些代码,它将搜索字符串并返回缺少的任何字母表。这是我所拥有的:

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("d a b c");

导致 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(' d a b c ')));

或两个 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(' d a b c ')));

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