如何检查字符串是否包含javascript中的所有字母 [英] How do I check if a string has all letters of alphabet in javascript

查看:83
本文介绍了如何检查字符串是否包含javascript中的所有字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程非常陌生,并开始使用JavaScript。

我很好奇是否有方法检查变量是否至少包含一次字母表的所有字母。



我尝试了什么:



所以我有变量



var string = prompt(输入消息,);



好​​奇如何检查是否输入的消息包含每个字母a到z。如果是警报(所有字母至少找到一次)

解决方案

尝试



 var alphabets26 ='abcdefghijklmnopqrstuvwxyz'; 
var input = prompt();
input = input.toLowerCase();
var icount = 0;
for(var i = 0; i< alphabets26.length; i ++){
var letter = alphabets26 [i];
if(input.indexOf(letter)> -1)
icount ++;
}
if(icount == 26)
alert('所有字母至少找到一次');

其他
警报('少数字母丢失');





演示: - JSFiddle [ ^ ]


< blockquote>你可以轻松实现自己,例如

  var  exists = []; 

var s = abcdefoo; // 输入字符串


for var i = 0 ; i < s.length; ++ i)
{
exists [s [i]] = 1 ;
}


for var i = 97 ; i< = 122 ; ++ i) / / 遍历所有小写字符
{
c = String 。 fromCharCode(ⅰ);
if (!exists [c])
{
alert(c + 缺少);
break ;
}
}


还有一个解决方案

 var Your_String =abcdefghijklmnopqrstuvwxyz ; 
var Result = Your_String.replace('','')。split('')。filter(function(item,i,ar){return ar.indexOf(item)=== i;})。加入('');
document.writeln(Result.length == 26?全部使用:有些遗失);



利用:javascript - 仅在字符串中显示一次唯一字符 - Stack Overflow [ ^ ]


Hi, I'm quite new to programming and started with JavaScript.
I'm curious if there is a method to check if a variable has all letters of the alphabet at least once.

What I have tried:

So I have the variable

var string = prompt("Type in message","");

Curious how to check if the message typed in has every letter a to z. if yes alert("All letters found at least once")

解决方案

try

var alphabets26 = 'abcdefghijklmnopqrstuvwxyz';
       var input = prompt();
       input = input.toLowerCase();
       var icount = 0;
       for (var i = 0; i < alphabets26.length; i++) {
           var letter = alphabets26[i];
           if (input.indexOf(letter) > -1)
               icount++;
       }
       if (icount == 26)
           alert('All letters found at least once');

       else
           alert('Few letters missing');



Demo : - JSFiddle[^]


You can easily implement yourself, for instance

var exists =  [];

var s = "abcdefoo"; // the input string


for (var i=0; i < s.length; ++i)
{
  exists[s[i]] = 1;
}


for ( var i = 97; i<=122; ++i) // loop over all lowercase characters
{
  c = String.fromCharCode(i);
  if ( !exists[c])
  {
    alert(  c + " is missing");
    break;
  }
}


One more solution

var Your_String= "abcdefghijklmnopqrstuvwxyz";
var Result = Your_String.replace(' ','').split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join('');
document.writeln(Result.length == 26 ? "All used" : "Some missing");


Made use of : javascript - Showing unique characters in a string only once - Stack Overflow[^]


这篇关于如何检查字符串是否包含javascript中的所有字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆