Java String Title Case titleCase(final String s)

Here you can find the source of titleCase(final String s)

Description

title Case

License

Open Source License

Declaration

public static String titleCase(final String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String titleCase(final String s) {
        if (s == null || s.length() == 0) {
            return s;
        } else if (s.length() == 1) {
            return s.toUpperCase();
        } else {/* w w  w  .j a  v a2 s .c o m*/
            StringBuilder result = new StringBuilder(s.length());
            String delim = "";
            for (String eachPart : s.split(" ")) {
                result.append(delim);
                delim = " ";
                if (eachPart.length() < 2) {
                    result.append(eachPart);
                } else {
                    result.append(eachPart.substring(0, 1).toUpperCase())
                            .append(eachPart.substring(1).toLowerCase());
                }
            }
            return result.toString();
        }
    }
}

Related

  1. titleCase(final String input)
  2. titleCase(final String text)
  3. titleCase(String in)
  4. titleCase(String input)
  5. titleCase(String name)