## Converting a char to uppercase

### Question

String lower = Name.toLowerCase();
int a = Name.indexOf(" ",0);
String first = lower.substring(0, a);
String last = lower.substring(a+1);
char f = first.charAt(0);
char l = last.charAt(0);
System.out.println(l);

how would i get the F and L variables converted to uppercase.

2016/04/15
1
74
4/15/2016 12:05:30 PM

Instead of using existing utilities, you may try below conversion using boolean operation:

To upper case:

char upperChar = 'l' & 0x5f

To lower case:

char lowerChar = 'L' ^ 0x20

How it works:

Binary, hex and decimal table:

------------------------------------------
| Binary   |   Hexadecimal     | Decimal |
-----------------------------------------
| 1011111  |    0x5f           |  95     |
------------------------------------------
| 100000   |    0x20           |  32     |
------------------------------------------

Let's take an example of small l to L conversion:

The binary AND operation: (l & 0x5f)

l character has ASCII 108 and 01101100 is binary represenation.

1101100
&  1011111
-----------
1001100 = 76 in decimal which is **ASCII** code of L

Similarly the L to l conversion:

The binary XOR operation: (L ^ 0x20)

1001100
^  0100000
-----------
1101100 = 108 in decimal which is **ASCII** code of l
2018/02/18

Have a look at the java.lang.Character class, it provides a lot of useful methods to convert or test chars.

2010/09/12

f = Character.toUpperCase(f);
l = Character.toUpperCase(l);
2010/09/12

System.out.println(first.substring(0,1).toUpperCase());
System.out.println(last.substring(0,1).toUpperCase());
2017/01/08

If you are including the apache commons lang jar in your project than the easiest solution would be to do:

WordUtils.capitalize(Name)

takes care of all the dirty work for you. See the javadoc here

Alternatively, you also have a capitalizeFully(String) method which also lower cases the rest of the characters.

2013/07/06