Results 1 to 17 of 17
  1. #1
    Valeriano is offline Member
    Join Date
    May 2007
    Posts
    10
    Rep Power
    0

    Default How to convert a String to upper case

    I am getting a string from the user and i need to convert it to upper case. How can i do this?

  2. #2
    levent Guest

    Default

    Hi Valeriano,

    You can use toUpperCase() method of String class. String class has lots of more useful methods.

  3. #3
    amateur83 is offline Member
    Join Date
    Jun 2007
    Posts
    3
    Rep Power
    0

    Default

    String s1 = "test";

    s1 = s1.toUpperCase();

    System.out.println(""=========RESULT=========" + s1);

    //it'll print TEST

  4. #4
    faisalcmpm is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default Try this

    private String conertToTitleCase(String actualName) {
    StringBuilder name = new StringBuilder(actualName);
    name.setCharAt(0, (char)(name.charAt(0) -32));
    for(int i = 1 ; i < name.length() ; i++){
    if(name.charAt(i -1) == ' ' && name.charAt(i) != ' '){
    name.setCharAt(i, (char)(name.charAt(i) -32));
    }
    }
    return name.toString();
    }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by faisalcmpm View Post
    private String conertToTitleCase(String actualName) {
    StringBuilder name = new StringBuilder(actualName);
    name.setCharAt(0, (char)(name.charAt(0) -32));
    for(int i = 1 ; i < name.length() ; i++){
    if(name.charAt(i -1) == ' ' && name.charAt(i) != ' '){
    name.setCharAt(i, (char)(name.charAt(i) -32));
    }
    }
    return name.toString();
    }
    And if the text is not US-ASCII?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    I saved this thinking one day I might need it (and I was right, look like today is the day)
    it works in C/C++ have not used it in java

    uppercase = lowercase ('a' 'A');
    lowercase = uppercase + ('a' 'A');

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    huh... "uppercase" and "lowercase" are string variables?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i'd assume chars.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    ah!! yes... duh... got it... thanks !
    ... and yeah, it works if you do some casting:
    uppercase = (char)(lowercase - ('a' - 'A'));
    lowercase = (char)(uppercase + ('a' - 'A'));
    But I think it will only support US ASCII.

    CJSL
    Last edited by CJSLMAN; 02-13-2009 at 09:41 PM.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by CJSLMAN View Post
    But I think it will only support US ASCII.
    Correct. It won't even support the extended ASCII characters.

  13. #13
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    zombieeee thread.

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    hahaha.... didn't even notice... well, at least it was fun !!! I don't feel so bad because it seems everybody really into it...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ezered View Post
    Create a program that converts all vowels to uppercase and all consonants to lowercase.

    Enter a String: HELLO WORLD
    Converted String: hEllO wOrld
    Please stop double posting and stick to your own thread.

    kind regards,

    Jos

  16. #16
    Andaji is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default

    You can use method...
    String toUpperCase( word you want to move to upper case goes in this area);
    then it will move all the words to upper case..
    I think this link below may help you about these stuffs.
    String (Java 2 Platform SE 5.0)


    Best regards

    Thank You !

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Andaji View Post
    You can use method...
    String toUpperCase( word you want to move to upper case goes in this area);
    then it will move all the words to upper case..
    I think this link below may help you about these stuffs.
    String (Java 2 Platform SE 5.0)
    Best regards
    This answer has already been given in this thread and three years ago. LOL. Let's try to keep things relevant. Locking thread.

Similar Threads

  1. String Title case
    By bugger in forum New To Java
    Replies: 6
    Last Post: 01-31-2012, 01:21 PM
  2. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 01:32 PM
  3. String manipulation example (Title case)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:04 AM
  4. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  5. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-14-2007, 11:26 PM

Posting Permissions

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