84

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);
2
  • which part of the answers to your previous question you didn't understand? My answer there contains an answer to this question.
    – Bozho
    Sep 12, 2010 at 20:24
  • @shep - Can you accept BalusC's answer? It will help future searchers. Oct 9, 2017 at 2:19

11 Answers 11

158

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.

45

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
3
  • 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. Sep 6, 2017 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. Sep 6, 2017 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, 2018 at 1:52
18

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

2
  • 2
    +1 I like the answers that providers the user with a reference to go seek the answer Sep 12, 2010 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, 2014 at 16:48
15
f = Character.toUpperCase(f);
l = Character.toUpperCase(l);
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

3
  • 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 8, 2015 at 3: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, 2015 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, 2015 at 8:49
5
System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());
2
  • 1
    Can you explain your answer? Jan 8, 2017 at 5:29
  • first.substring(0,1) will retrieve substring which is at 0 index of string and then applying toUpperCase method to that string which will give you the first character in that string as uppercase Jan 8, 2017 at 13:59
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.

1
  • The link you provided gives 404 error!!!! can you correct it??
    – Visruth
    Jul 5, 2013 at 10:50
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());
1

Lets assume you have a variable you want split

String name = "Your name variable";
char nameChar = Character.toUpperCase(name.charAt(0));
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();
}
-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.