-1

I have multiple strings in upper case:

JAMES SMITH, NAVIN HEMANT SIGNH, LALA UTRIZER

How can I transform these strings to:

James Smith, Navin Hemant Signh, Lala Utrizer in java?

Please help.

Doez16
  • 29
  • 4
  • 1
    by writing code to do so, instead of asking for it. there are several ways to do this, all of which are extremely basic. write down a scenario in which it would possible (not code, just pseudo-code), so you have the logic, and after that, implement it – Stultuske Sep 14 '16 at 10:48
  • Refer the link below for the same http://stackoverflow.com/questions/3904579/how-to-capitalize-the-first-letter-of-a-string-in-java – Abhishek Sep 14 '16 at 10:50
  • This is a duplicate question. You can find the answer here: http://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string – Plee Sep 14 '16 at 10:54

2 Answers2

2

Try something like this:

String values = "JAMES SMITH, NAVIN HEMANT SIGNH, LALA UTRIZER";


if (values != null && !values.isEmpty()) {
    final StringBuilder fixedCaseBuilder = new StringBuilder(values.length()+1);

    for (final String word : values.split(" ")) {
        if (!word.isEmpty()) {
            fixedCaseBuilder.append(Character.toUpperCase(word.charAt(0)));
            fixedCaseBuilder.append(word.substring(1, word.length()).toLowerCase());
        }
        fixedCaseBuilder.append(' ');
    }

    fixedCaseBuilder.setLength(fixedCaseBuilder.length()-1);
    final String result = fixedCaseBuilder.toString();
}
huellif
  • 414
  • 4
  • 12
1

If you can choose what libraries to use, I would advise WordUtils from Apache Commons Utils. It has a method capitalizeFully(String s), which takes a String and capitalize the first letter of every word.

Vaidas
  • 1,494
  • 14
  • 21