SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast Hervard's Avatar
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to count the number of uppercase letters in a string?

    Is there a way to count the number of uppercase letters in a string using Javascript?

    I could go through each letter in the alphabet and compare it with each letter in the string, but I think this would be too slow.

    Or, is there a regular expression that could tell if a string contained more than one uppercase letter?

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    something like this should work:

    HTML Code:
    <script type="text/javascript">
    str = "ThIs Is A Test On HOW tO counT UppeR CaSE";
    alert(str.replace(/[^A-Z]/g, "").length);
    </script>
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's more than one way to do that. Here's one:
    Code:
    function countUpperCaseChars(str) {
      var count=0,len=str.length;
      for(var i=0;i<len;i++) {
        if(/[A-Z]/.test(str.charAt(i))) count++;
      }
      return count;
    }
    Edit: *chuckles* It seems that ALL thought of a more efficient way to do it.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Enthusiast Hervard's Avatar
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ALL View Post
    something like this should work:

    HTML Code:
    <script type="text/javascript">
    str = "ThIs Is A Test On HOW tO counT UppeR CaSE";
    alert(str.replace(/[^A-Z]/g, "").length);
    </script>
    This works beautifully - thank you, ALL!

    And yes, there is always more than one way to do something. Thanks for replying as well, Kravvitz

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not using regexes:
    Code:
    var str = "ThIs Is A Test";
    for(var i = 0, len = str.length, count=0, ch; i < len; ++i)
    {
    	ch = str.charAt(i);
    	if(ch >= 'A' && ch <= 'Z') ++count;
    }
    
    alert(count);

  6. #6
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It don't show turkish characters (&#214;, &#220;, &#199;, İ, Ğ, Ş). Turkish characters will be added this. How? Thanks...
    Code:
     
    <script type="text/javascript">
    str = "&#220;&#199; tas has hoŞaf g&#214;t&#252;r&#252;ld&#252;.";
    alert(str.replace(/[^A-Z]/g, "").length);  // 0
    </script>
    Last edited by muazzez; Feb 4, 2007 at 14:43.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    muazzez - Unfortunately, you would have to define an array of non-latin uppercase characters and do an individual character compare.

  8. #8
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by muazzez View Post
    It don't show turkish characters (Ö, Ü, Ç, I, Ğ, Ş). Turkish characters will be added this. How?
    Code:
     
    <script type="text/javascript">
    str = "ÜÇ tas has hoŞaf gÖtürüldü.";
    alert(str.replace(/[^A-Z]/g, "").length);  // 0
    </script>
    actually yes you can but you have to add those speical characters by hand...

    First you will need to findout if the character is within the 256 ascii characters and if not you will need to find out the unicode character of each one. the only two in your example that needed where the last two.

    then do something like this:
    HTML Code:
    <script type="text/javascript">
    str = "ÜÇ tas has hoaf"+String.fromCharCode(350)+" gÖtürüldü.";
    regex = new RegExp('[^A-Z\Ö\Ç\Ü\I\\' + String.fromCharCode(350) + '\\' + String.fromCharCode(286) + ']', 'g');
    alert(str.replace(regex, "").length);  // 0
    </script>
    I used String.fromCharCode(350) for Ğ character because my browser didnt like me putting in unique characters, but if you are deriving it though a textbox you shouldn not need to worry about it. I aluse used \ characters more for a "just incase" browser compatability problem.

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  9. #9
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ALL,
    Thanks for your code.
    I tried your code. Message box showed 4.
    Here is the code I wrote.
    ÜÇ tas has hoŞaf gÖtürüldü konaĞın İçine.
    Code:
     
    <script type="text/javascript">
    
    str = "ÜÇ tas has hoŞaf gÖtürüldü konaĞın İçine.";
    
    alert(str.replace(/[^A-ZÜÖÇŞiĞ]/g, "").length);  // 6
    
    </script>
    It works on Opera 9.10, Firefox 2.0.0.1 and Internet Explorer 7.
    But I don't sure [^A-ZÜÖÇŞiĞ] is correct.

  10. #10
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    that should be fine, but using non-ascii characters might act funny in old browsers.
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •