Nanda
Nanda

Reputation: 1058

How do I convert strings between uppercase and lowercase in Java?

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

Upvotes: 27

Views: 129948

Answers (6)

Jaime Montoya
Jaime Montoya

Reputation: 7711

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 + " ");  
    } 
  } 
}

Upvotes: 1

zubin patel
zubin patel

Reputation: 19

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

Upvotes: 0

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.

Upvotes: 3

Crickey
Crickey

Reputation: 381

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

Upvotes: 23

PeterMmm
PeterMmm

Reputation: 24640

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

Upvotes: 35

Fortega
Fortega

Reputation: 19702

String#toLowerCase

Upvotes: 2

Related Questions