Java String Capitalize capitalize(String s)

Here you can find the source of capitalize(String s)

Description

capitalize

License

Open Source License

Declaration

public static String capitalize(String s) 

Method Source Code

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

public class Main {
    public static String capitalize(String s) {
        if (s == null) {
            return null;
        }/*from   w  w w .  ja  v a  2  s.  c  om*/

        String[] words = s.split(" ");
        StringBuffer buffer = new StringBuffer();

        for (String word : words) {
            if (buffer.length() > 0) {
                buffer.append(' ');
            }
            buffer.append(capitalizeWord(word));
        }

        return buffer.toString();
    }

    private static String capitalizeWord(String s) {
        if (s.length() > 1) {
            return s.substring(0, 1).toUpperCase() + s.substring(1, s.length()).toLowerCase();
        }

        return s.toUpperCase();
    }
}

Related

  1. capitalize(String s)
  2. capitalize(String s)
  3. capitalize(String s)
  4. capitalize(String s)
  5. capitalize(String s)
  6. capitalize(String s)
  7. capitalize(String s)
  8. capitalize(String s)
  9. capitalize(String s)