-3

I am trying to get the first character of a string to convert into an upper case. How is this possible?

For example;

String c = "hi steve, how are you doing?";

I want the 'h' of 'hi' capitalized. How do I do that using code? Thank you in advanced!

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    Using code, you use code. Look at the `String` javadoc. – Sotirios Delimanolis Mar 07 '14 at 20:56
  • Just first character of entire string? Or for each word? – Rohit Jain Mar 07 '14 at 20:56
  • 2
    You can use `substring` and `toUpperCase` methods from String class. You can also use `Character` class for this task. – Pshemo Mar 07 '14 at 20:56
  • 5
    `System.out.println("Hi steve, how are you doing?");` – Reimeus Mar 07 '14 at 20:57
  • Welcome to StackOverflow. You will find it's not a "Do my homework for me" service. – Brian Roach Mar 07 '14 at 20:59
  • @BrianRoach I am asking an actual question here. I never learned how to convert the first character of a string capitalized, I couldn't find it anywhere- so that's why I am here now. – user3368814 Mar 07 '14 at 21:01
  • In StackOverflow you need to show that you have done some effort by yourself. "What have you tried?" is a popular reply to these kind of questions -- see http://mattgemmell.com/what-have-you-tried/. – ZeroOne Mar 07 '14 at 21:02
  • 1
    Be sure to visit again for the second character... and especially the third one, cause that one's much trickier. – Marko Topolnik Mar 07 '14 at 21:03
  • @user3368814 you're being ridiculed because you asked an extremely basic question you could certainly find the answer to with more effort. Sorry, StackOverflow doesn't look kindly on that. Hence the downvotes. – djechlin Mar 07 '14 at 21:03

3 Answers3

1
c = Character.toUpperCase(c.charAt(0))+c.substring(1);
Hat
  • 11
  • 1
1

You can use String's charAt method to get the first character and the Character toUpperCase method to convert it to upper case. Then use the substring method to get everything after the first character.

String c = "hi steve, how are you doing?";
c = Character.toUpperCase(c.charAt(0)) + c.substring(1);
System.out.println(c);
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
0

Use of stringbuilder creates less strings:

StringBuilder s1 = new StringBuilder("hi steve");
s1.replace(0, s1.length(), s1.toString().toLowerCase());
s1.setCharAt(0, Character.toTitleCase(s1.charAt(0)));
T McKeown
  • 12,971
  • 1
  • 25
  • 32