Java String Capitalize capitalize(String str)

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

Description

capitalize

License

Open Source License

Declaration

public static String capitalize(String str) 

Method Source Code

//package com.java2s;
/*/* w  w w .j  av a 2 s  .  c o  m*/
 * (c) Copyright 2010-2011 AgileBirds
 *
 * This file is part of OpenFlexo.
 *
 * OpenFlexo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenFlexo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    public static String capitalize(String str) {
        if (isAlpha(str.charAt(0))) {
            return str.substring(0, 1).toUpperCase() + str.substring(1);
        } else
            // if starts with '_' for example, capitalize 2nd character
            return str.charAt(0) + str.substring(1, 2).toUpperCase() + str.substring(2);
    }

    /**
     * Returns true if ch matches [A-Za-z] http://www.asciitable.com
     * 
     * @param ch
     * @return
     */
    public static boolean isAlpha(char ch) {
        // returns true if ch matches [A-Za-z]
        return ((ch > (char) 64 && ch < (char) 91) || (ch > (char) 96 && ch < (char) 123));
    }
}

Related

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