How to compare a character in a string with an upper case letter?

var str=new String(); str=prompt('Введите строку'); var n=str.length for (var i=0; i<n; i++) { if (str.charAt(i)==["AZ"]) { document.write (str.charAt(i)) } } 

I want the upper case characters to appear on the page, I don’t know how to correctly write the symbol comparison with the upper case letters

  • line contains only Latin ( A-Za-z )? - hindmost
  • Error: A - no definition - hik

3 answers 3

If I understand you correctly, then it seems to me somehow (within your cycle):

 var n=str.length, html = ''; for (var i=0; i<n; i++) { var s = str.charAt(i), S=s.toUpperCase(); if(S==s && S!= S.toLowerCase() ){html+=S;} // S!= S.toLowerCase() - отсекает всякие запятые и прочий хлам. ( Sergey Snegirev, благодарю за подсказку!) } document.write (html); 

Or search in the string with the use of regular degenerations, but this I still poorly imagine how ...

  • This code will also output a comma, for example =) - Sergey Snegirev
  • one
    if (s == s.toUpperCase () && s.toUpperCase ()! = s.toLowerCase ()) {... this is how punctuation marks are cut off, etc. - Sergey Snegirev
  • hm, I agree. In this case, then it will be if (S == s && S! = S.toLowerCase ()) {} - Grinya Lesnoy
  • Corrected, thank you for the tip! - Grinya Lesnoy
 if ( "ABCDEFGHIJKLMNOPQRSTUVXYZ".indexOf( str.charAt(i) ) > -1) { // do something } 

    If you need to pull out only capital letters from the source line, you can do this:

     var str = prompt('Введите строку'), regexp = /[^AZ]/g, result = str.replace(regexp, ''); alert(result); 

    As a result, there will be only Latin capital letters.