Java String Uncapitalize uncapitalize(final CharSequence sequence)

Here you can find the source of uncapitalize(final CharSequence sequence)

Description

Uncapitalizes a character sequence changing the first letter to title case as per Character#toLowerCase(char) .

License

Apache License

Parameter

Parameter Description
sequence the character sequence to uncapitalize, may be null

Return

the uncapitalized character sequence, null if null String input

Declaration

public static CharSequence uncapitalize(final CharSequence sequence) 

Method Source Code


//package com.java2s;
/*//from w  w  w . j a  v a2s .c o m
 * Copyright 2016 Dmitry Korotych (dkorotych at gmail dot 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.
 */

import java.util.function.Predicate;
import java.util.function.UnaryOperator;

public class Main {
    /**
     * Uncapitalizes a character sequence changing the first letter to title
     * case as per {@link Character#toLowerCase(char)}. No other letters are
     * changed. A {@code null} input String returns {@code null}.
     * <pre>
     * CharSequenceUtils.uncapitalize(null)  = null
     * CharSequenceUtils.uncapitalize("")    = ""
     * CharSequenceUtils.uncapitalize("Cat") = "cat"
     * CharSequenceUtils.uncapitalize("CAT") = "cAT"
     * </pre>
     *
     * @param sequence the character sequence to uncapitalize, may be null
     * @return the uncapitalized character sequence, {@code null} if null String
     *     input
     * @see #capitalize(java.lang.CharSequence)
     */
    public static CharSequence uncapitalize(final CharSequence sequence) {
        return modifyFirstChar(sequence, Character::isLowerCase, Character::toLowerCase);
    }

    /**
     * Modify the first character of the input string.
     * @param sequence the character sequence to modification
     * @param predicate function to check of the first character to the need for
     *     modification
     * @param processing function of modification
     * @return The altered sequence
     */
    private static CharSequence modifyFirstChar(final CharSequence sequence, final Predicate<Character> predicate,
            final UnaryOperator<Character> processing) {
        if (isEmpty(sequence)) {
            return sequence;
        }
        final char firstChar = sequence.charAt(0);
        if (predicate.test(firstChar)) {
            return sequence;
        }
        return String.valueOf(processing.apply(firstChar)) + sequence.subSequence(1, sequence.length());
    }

    /**
     * Returns {@code true} if this sequence is {@code null} or it contains no
     * elements.
     *
     * @param sequence the character sequence, may be null
     * @return {@code true}, if this sequence is {@code null} or it contains no
     *     elements
     */
    public static boolean isEmpty(final CharSequence sequence) {
        return nullToEmpty(sequence).length() == 0;
    }

    /**
     * Returns input sequence or empty string if sequence is {@code null}.
     *
     * @param sequence the character sequence, may be null
     * @return input sequence or empty string if sequence is {@code null}
     */
    public static CharSequence nullToEmpty(final CharSequence sequence) {
        if (sequence == null) {
            return "";
        } else {
            return sequence;
        }
    }
}

Related

  1. uncapitalize(CharSequence cs)
  2. uncapitalize(final CharSequence s)
  3. uncapitalize(final String s)
  4. uncapitalize(final String str)
  5. uncapitalize(final String str)
  6. uncapitalize(final String str)