Java String Capitalize First capitalizeFirstOnly(String s)

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

Description

Capitalize all letters preceded by whitespace, and lower case all other letters.

License

Open Source License

Parameter

Parameter Description
s the String to capitalize

Return

the capitalized string

Declaration

public static String capitalizeFirstOnly(String s) 

Method Source Code

//package com.java2s;
/*//from  ww  w.  jav  a 2  s  .c  o  m
 * #%L
 * Cytoscape Work Swing Impl (work-swing-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

public class Main {
    /**
     * Capitalize all letters preceded by whitespace, and lower case
     * all other letters. 
     * @param s the String to capitalize
     * @return the capitalized string
     */
    public static String capitalizeFirstOnly(String s) {
        if (s == null)
            return null;
        if (s.length() == 0)
            return s;

        StringBuffer sbuf = new StringBuffer();
        char c = s.charAt(0);
        sbuf.append(Character.toUpperCase(c));
        boolean space = Character.isWhitespace(c);
        for (int i = 1; i < s.length(); ++i) {
            c = s.charAt(i);
            if (Character.isWhitespace(c)) {
                space = true;
            } else if (space) {
                c = Character.toUpperCase(c);
                space = false;
            } else {
                c = Character.toLowerCase(c);
            }
            sbuf.append(c);
        }
        return sbuf.toString();
    }
}

Related

  1. capitalizeFirstLetter(String string)
  2. capitalizeFirstLetter(String string)
  3. capitalizeFirstLetter(String text)
  4. capitalizeFirstLetter(String[] strArr)
  5. capitalizeFirstOnly(String original)
  6. capitalizeTheFirstLetter(String input)