Converting a char to uppercase

JavaCharUppercase

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

f = Character.toUpperCase(f);
l = Character.toUpperCase(l);

Solution 5 - Java

Since you know the chars are lower case, you can subtract the according ASCII value to make them uppercase:

char a = 'a';
a -= 32;
System.out.println("a is " + a); //a is A

Here is an ASCII table for reference

Solution 6 - Java

System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());

Solution 7 - Java

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.

Solution 8 - Java

You can apply the .toUpperCase() directly on String variables or as an attribute to text fields. Ex: -

String str;
TextView txt;

str.toUpperCase();// will change it to all upper case OR
txt.append(str.toUpperCase());
txt.setText(str.toUpperCase());

Solution 9 - Java

I think you are trying to capitalize first and last character of each word in a sentence with space as delimiter.

Can be done through StringBuffer:

public static String toFirstLastCharUpperAll(String string){
    StringBuffer sb=new StringBuffer(string);
        for(int i=0;i<sb.length();i++)
        	if(i==0 || sb.charAt(i-1)==' ' //for first character of string/each word
   	    		|| i==sb.length()-1 || sb.charAt(i+1)==' ') //for last character of string/each word
   	    		sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
   	 return sb.toString();
}

Solution 10 - Java

Lets assume you have a variable you want split

String name = "Your name variable";
char nameChar = Character.toUpperCase(name.charAt(0));

Solution 11 - Java

The easiest solution for your case - change the first line, let it do just the opposite thing:

String lower = Name.toUpperCase ();

Of course, it's worth to change its name too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshepView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaRahul SharmaView Answer on Stackoverflow
Solution 3 - JavaAndreas DolkView Answer on Stackoverflow
Solution 4 - JavaDaveJohnstonView Answer on Stackoverflow
Solution 5 - JavaBurritoView Answer on Stackoverflow
Solution 6 - JavaLikhith KumarView Answer on Stackoverflow
Solution 7 - JavaAsafView Answer on Stackoverflow
Solution 8 - Javaامون بسامView Answer on Stackoverflow
Solution 9 - JavaPraveenView Answer on Stackoverflow
Solution 10 - JavaMUGABAView Answer on Stackoverflow
Solution 11 - JavaRomanView Answer on Stackoverflow