Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript String to uppercase and lowercase???

    Hi,

    I want to create the following output:
    Kalle Anka
    kALLE aNKA
    K#lle #nka
    I know how to create "Kalle Anka" and "K#lle #nka".

    But how do I do to change the lowercase letters to uppercase and the uppercase letters to lowercase?

    Kalle Anka
    kALLE aNKA
    K#lle #nka
    THANKS!

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Code:
    urstring.toLowerCase();
    urstring.toUpperCase();

  • #3
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    function swapCase(ch) { 
      return /[a-z]/.test(ch) ? ch.toUpperCase() : ch.toLowerCase(); 
    }
    str = 'Kalle Anka';
    arr = str.split('');
    trans_str = arr.map(swapCase).join('');

  • #4
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BarrMan View Post
    Code:
    urstring.toLowerCase();
    urstring.toUpperCase();
    Hi "BarrMan"!

    Thanks, but I already know that (Maybe I should have written that?).

    I need to know how to implement this in my code so that "Kalle Anka" becomes "kALLE aNKA".

    Maybe using a for or if statement?

    Thoughts, ideas and tips are welcomed!

  • #5
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    oh and in case u want to run it in IE use a for/while loop instead of the map sorta like

    Code:
    for ( i = 0; i < arr.length; i++ ) {
      arr[i] = swapCase(arr[i]);
    }

  • #6
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks "shyam"!

    Code:
      return /[a-z]/.test(ch) ? ch.toUpperCase() : ch.toLowerCase();
    Can you please explain your code? Im very new to javascript and would like to know how it all works.

  • #7
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    If I'm not wrong, the function checks if each letter of the string is in lowercase (a-z), if the character is in lowercase - make it uppercase, else make it lowercase.
    Code:
    statement ? result1 : result2
    This is a short of:
    Code:
    if(statement)
    result1
    else
    result2

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    This question was answered recently by Vic Phillips

    http://www.codingforums.com/showthread.php?t=107641

  • #9
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    return /[a-z]/.test(ch) ? ch.toUpperCase() : ch.toLowerCase();
    putting stuff between /'s is short-hand for the getting a RegExp object and [a-z] matches only the lowercase characters as u've already guessed by now and BarrMan has explained the ternary operator too ... so, there u go


  •  

    Posting Permissions

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