-2

I am trying figure out how to change the character at position 0 of a string to uppercase if it is lower case. I am sorry if this is a dumb question, but it is stumping me. Something like this:

String myString = "hello";
if( //string position 0 is lowercase )
{
    char myChar = myString.charAt(0);
    myChar.toUpperCase();
}

I think .toUpperCase() only works for strings though. Any help would be appreciated!

Harry Manback
  • 41
  • 1
  • 6

2 Answers2

0

You can use Character.isUpperCase() to check if a character is upper case.

You can use Character.toUpperCase() to convert a single character to upper case.

tehp
  • 5,018
  • 1
  • 24
  • 31
0

Think of changing myString to a char array and working it from that. The Character package in Java has some helpful functions:

String myString = "hello";
char[] sArr = myString.toCharArray();

sArr[0] = Character.toUpperCase(sArr[0]);

myString = new String(sArr);

EDIT: As pointed out by Scary Wombat, there is no need to check if the first char is upper or not. Simply call the Character.toUpperCase(sArr[0]). Whether it is upper or not doesn't effect the outcome.