Java String Capitalize capitalize(String s)

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

Description

Capitalize a string, producing a valid Java identifier.

License

Open Source License

Declaration

public static final String capitalize(String s) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * The contents of this file are subject to the Ricoh Source Code Public
 * License Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.risource.org/RPL/* w w w . ja  v  a2 s  .  c  o  m*/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * This code was initially developed by Ricoh Innovations, Inc.  Portions
 * created by Ricoh Innovations, Inc. are Copyright (C) 1995-1999.  All
 * Rights Reserved.
 *
 * Contributor(s):
 *
 ***************************************************************************** 
*/

public class Main {
    /** Capitalize a string, producing a valid Java identifier.  
     *   Any character which is not a letter or digit is deleted and the next
     *   character is capitalized.  Uppercase characters are preserved.
     */
    public static final String capitalize(String s) {
        StringBuffer n = new StringBuffer();
        boolean capitalize = true;
        for (int i = 0; i < s.length(); ++i) {
            if (!Character.isLetterOrDigit(n.charAt(i)))
                capitalize = true;
            else {
                n.append(capitalize ? Character.toUpperCase(s.charAt(i)) : s.charAt(i));
                capitalize = false;
            }
        }
        return n.toString();
    }
}

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)