26

What is the method for converting strings in Java between upper and lower case?

1
  • 14
    develop the habit of reading the API documentation (Javadocs) that way you will get first hand information.
    – Samuh
    Dec 23, 2009 at 10:20

6 Answers 6

34

String#toLowerCase and String#toUpperCase are the methods you need.

3
  • Although 'toUpperCase' will give the opposite effect, I'll still give you +1 :-)
    – Fortega
    Dec 23, 2009 at 10:11
  • Fixed (including linking to Java6 docs - does anyone even still use 1.4?) and upvoted.
    – paxdiablo
    Dec 23, 2009 at 10:38
  • 1
    @Fortega - the OP said "converting between" not "converting from / to". Converting between implies converting in both directions.
    – Stephen C
    Dec 23, 2009 at 12:29
23

There are methods in the String class; toUppercase() and toLowerCase().

i.e.

String input = "Cricket!";
String upper = input.toUpperCase(); //stores "CRICKET!"
String lower = input.toLowerCase(); //stores "cricket!" 

This will clarify your doubt

0
3

Yes. There are methods on the String itself for this.

Note that the result depends on the Locale the JVM is using. Beware, locales is an art in itself.

4
  • Yup. I recently discovered that the size of string is not always the same as the size of string.toUpperCase()
    – Fortega
    Dec 23, 2009 at 10:19
  • Yes but only in those weird languages with umlauts and Eszett and names like Thorbjørn :-)
    – paxdiablo
    Dec 23, 2009 at 10:33
  • 2
    you mean like weird languages like every other language on the planet?
    – jim
    Dec 23, 2009 at 11:04
  • 4
    Actually we use ISØ-Låtin-1 here in Sĉändïñävïä so it could be a LØT WØRSË :) Dec 23, 2009 at 11:55
2

String#toLowerCase

1

Assuming that all characters are alphabetic, you can do this:

From lowercase to uppercase:

// Uppercase letters. 
class UpperCase {  
  public static void main(String args[]) { 
    char ch;
    for(int i=0; i < 10; i++) { 
      ch = (char) ('a' + i);
      System.out.print(ch); 

      // This statement turns off the 6th bit.   
      ch = (char) ((int) ch & 65503); // ch is now uppercase
      System.out.print(ch + " ");  
    } 
  } 
}

From uppercase to lowercase:

// Lowercase letters. 
class LowerCase {  
  public static void main(String args[]) { 
    char ch;
    for(int i=0; i < 10; i++) { 
      ch = (char) ('A' + i);
      System.out.print(ch);
      ch = (char) ((int) ch | 32); // ch is now uppercase
      System.out.print(ch + " ");  
    } 
  } 
}
0

Coverting the first letter of word capital

input:

hello world

String A = hello;
String B = world;
System.out.println(A.toUpperCase().charAt(0)+A.substring(1) + " " + B.toUpperCase().charAt(0)+B.substring(1));

Output:

Hello World

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.