Java String Capitalize capitalize(String s)

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

Description

/ Returns a capitalized String.

License

Apache License

Parameter

Parameter Description
s String to capitalize

Return

a capitalized String

Declaration

public static String capitalize(String s) 

Method Source Code

//package com.java2s;
/**/*from  ww  w .  ja  v a  2 s  .  com*/
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  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.
 **/

public class Main {
    /**
    /**
     * Returns a capitalized String.
     * Make the first character upper case leaving untouched the other ones
     * Eg: this is my String -> This is my String
     *
     * @param s String to capitalize
     * @return a capitalized String
     */
    public static String capitalize(String s) {
        if (isEmpty(s)) {
            return s;
        }
        if (s.length() == 1) {
            return "" + Character.toUpperCase(s.charAt(0));
        }
        return "" + Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }

    /**
     * Checks if a String is empty or null
     *
     *   @param s String to check
     * @return true if null or empty, false otherwise
     */
    public static boolean isEmpty(String s) {
        return (s == null || "".equals(s.trim()));
    }

    /**
     *
     * @param s
     * @param alt
     * @return
     */
    public static String isEmpty(String s, String alt) {
        if (isEmpty(s)) {
            return alt;
        }
        return s;
    }

    /**
     * Esegue una toUpperCase su una Stringa
     * se s == null return null
     *
     *   @param s String to toUpperCase
     * @return La Stringa toUpperCase
     */
    public static final String toUpperCase(String s) {
        if (s == null) {
            return null;
        }
        return s.toUpperCase();
    }

    /**
     *
     * @param s
     * @param start
     * @param stop
     * @return string
     */
    public static final String substring(String s, int start, int stop) {
        if (isEmpty(s)) {
            return s;
        }
        if (start < 0 || start >= s.length()) {
            return "";
        }
        if (stop > s.length() || stop < 0) {
            stop = s.length();
        }
        return s.substring(start, stop);
    }

    /**
     *
     * @param s
     * @param start
     * @return string
     */
    public static final String substring(String s, int start) {
        return substring(s, start, -1);
    }

    /**
     * Esegue una Trim su una Stringa
     * se s == null return null
     *
     *   @param s String to trim
     * @return La Stringa Trimmata
     */
    public static final String trim(String s) {
        if (s == null) {
            return null;
        }
        return s.trim();
    }
}

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)