-5

List item

How can I create a private method called "niceLetters" which takes String in any uppercase/lowercase combination and return only the first letter uppercased and all other letters lowercased?

e.g.

1.) niceLetters("stEPheN") returns "Stephen"

2.) niceLetters("HELLO") returns "Hello"

3.)niceLEtters(null) returns null

4.)niceLetters("") returns ""

  • 3
    StackOverFlow is not a place where you ask people to do your homework.This kind of question is not only obviously a homework question but also quite trivial and show you have not researched the answer before posting here. – Asura May 19 '15 at 03:23
  • possible duplicate of [Capitalize First Char of Each Word in a String Java](http://stackoverflow.com/questions/1892765/capitalize-first-char-of-each-word-in-a-string-java) – Val May 19 '15 at 03:25
  • you're such a genius. It's not homework and I did research the answer with no success. – supergnagno May 19 '15 at 06:13

1 Answers1

0
private static String niceLetters(String str) {
    if (str == null) {
        return null;
    }

    if (str.trim().length() <= 1) {
        return str.trim().toUpperCase();
    }

    return (Character.toUpperCase(str.trim().charAt(0))+str.trim().substring(1).toLowerCase());
}
kaykay
  • 556
  • 2
  • 7