Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a char to uppercase

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.

like image 292
shep Avatar asked Sep 12 '10 20:09

shep


People also ask

How do you make a char uppercase in C++?

The toupper() function in C++ converts a given character to uppercase. It is defined in the cctype header file.

How do you change all characters to uppercase in Java?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.

How do you convert a character to uppercase in Python?

Performing the . upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.

How do you convert a lowercase character to uppercase in Java?

The java. lang. Character. toLowerCase(char ch) converts the character argument to lowercase using case mapping information from the UnicodeData file.


2 Answers

You can use Character#toUpperCase() for this.

char fUpper = Character.toUpperCase(f); char lUpper = Character.toUpperCase(l); 

It has however some limitations since the world is aware of many more characters than can ever fit in 16bit char range. See also the following excerpt of the javadoc:

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toUpperCase(int) method.

like image 175
BalusC Avatar answered Oct 03 '22 18:10

BalusC


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 
like image 30
Rahul Sharma Avatar answered Oct 03 '22 17:10

Rahul Sharma