Java String Capitalize capitalize(String string0)

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

Description

Safely capitalizes a string by converting the first character to upper case.

License

Apache License

Parameter

Parameter Description
string0 The string to capitalize

Return

A new string with the first character converted to upper case

Declaration

static public String capitalize(String string0) 

Method Source Code

//package com.java2s;
/*//from ww  w . j  a v  a 2s  .c om
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

public class Main {
    /**
     * Safely capitalizes a string by converting the first character to upper
     * case. Handles null, empty, and strings of length of 1 or greater.  For
     * example, this will convert "joe" to "Joe".  If the string is null, this
     * will return null.  If the string is empty such as "", then it'll just
     * return an empty string such as "".
     * @param string0 The string to capitalize
     * @return A new string with the first character converted to upper case
     */
    static public String capitalize(String string0) {
        if (string0 == null) {
            return null;
        }
        int length = string0.length();
        // if empty string, just return it
        if (length == 0) {
            return string0;
        } else if (length == 1) {
            return string0.toUpperCase();
        } else {
            StringBuilder buf = new StringBuilder(length);
            buf.append(string0.substring(0, 1).toUpperCase());
            buf.append(string0.substring(1));
            return buf.toString();
        }
    }
}

Related

  1. capitalize(String string)
  2. capitalize(String string)
  3. capitalize(String string)
  4. capitalize(String string, boolean firstWordOnly)
  5. capitalize(String string, int index)
  6. capitalize(String text)
  7. capitalize(String text)
  8. capitalize(String text)
  9. capitalize(String text)