85

How could I get the f and l variables converted 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);
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
shep
  • 869
  • 2
  • 8
  • 6

11 Answers11

159

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
46

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
Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91
  • 14
    I'm honestly shocked that this has 7 votes. While I applaud the clever approach, being clever rarely leads to maintainable code, especially when used in place of a built-in method like `Character.toUpperCase()`. Any users of this should understand it will not handle anything non-ASCII. – Adam Hewitt Sep 06 '17 at 15:52
  • 1
    @AdamHewitt this approach work with non-ASCII chars as well e.g. '250', however, few non-ASCII characters wouldn't give expected results.Your point is correct that users should understand right usage of this approach and it should be used mainly for English alphabets. – Rahul Sharma Sep 06 '17 at 17:37
  • 1
    I think it should be `c | 0x20` instead of `c ^ 0x20` for lower case. Otherwise, if the character is already lower case, your code will convert it to uppercase – Steven Yue Feb 16 '18 at 01:52
19

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

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 2
    +1 I like the answers that providers the user with a reference to go seek the answer – Anthony Forloney Sep 12 '10 at 20:24
  • 3
    -1, if we dragnet for students we remove one of SO main benefits. Not spending inordinate amounts of time following peoples nested references across broken links. – ebt Jan 18 '14 at 16:48
15
f = Character.toUpperCase(f);
l = Character.toUpperCase(l);
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
6

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

Burrito
  • 1,475
  • 19
  • 27
  • 4
    This only works if the string is composed exclusively of ASCII characters. Languages like French, Greek, Spanish, Turkish, etc, have non-ASCII characters with upper/lower forms. This approach wouldn't work in those cases... – Mike Laren Feb 08 '15 at 03:08
  • Actually this basic approach *does* work for the most common non-ASCII characters in French, Spanish, German... e.g. `é` `à` `ö` `û` `ñ` ... So if the OP knows that he will only have to deal with such characters, he can stick to this method for the sake of simplicity and performance. – Sébastien Oct 13 '15 at 13:18
  • 1
    Doing calculations with characters to change the case is one of the worst habits, and so not 21st century! There is more than ascii chars! – Floyd Dec 17 '15 at 08:49
5
System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Likhith Kumar
  • 161
  • 4
  • 9
2

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());
QoP
  • 27,388
  • 16
  • 74
  • 74
2

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.

Asaf
  • 6,384
  • 1
  • 23
  • 44
  • The [link](http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/WordUtils.html#capitalize%28java.lang.String%29) you provided gives 404 error!!!! can you correct it?? – Visruth Jul 05 '13 at 10:50
1

Lets assume you have a variable you want split

String name = "Your name variable";
char nameChar = Character.toUpperCase(name.charAt(0));
MUGABA
  • 751
  • 6
  • 7
0

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();
}
Praveen
  • 1,791
  • 3
  • 20
  • 33
-1

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.

Roman
  • 64,384
  • 92
  • 238
  • 332