Java String Capitalized Check isCapitalizedCase(CharSequence source, char... delimiters)

Here you can find the source of isCapitalizedCase(CharSequence source, char... delimiters)

Description

is Capitalized Case

License

Open Source License

Declaration

public static boolean isCapitalizedCase(CharSequence source, char... delimiters) 

Method Source Code

//package com.java2s;

import static java.lang.Character.*;

public class Main {
    public static boolean isCapitalizedCase(CharSequence source, char... delimiters) {
        return !isEmpty(source) && equals(capitalizedCase(source.toString(), delimiters), source);
    }//www  . ja v a2  s  . co m

    public static boolean isEmpty(CharSequence source) {
        return source == null || source.length() == 0;
    }

    public static boolean equals(CharSequence source1, CharSequence source2) {
        return source1 == null ? source2 == null : source1.equals(source2);
    }

    public static String capitalizedCase(CharSequence source) {
        return capitalizedCase(source, null);
    }

    public static String capitalizedCase(CharSequence source, char... delimiters) {
        int delimitersCount = delimiters == null ? -1 : delimiters.length;
        if (isEmpty(source) || delimitersCount == 0) {
            return source.toString();
        }
        char[] buffer = source.toString().toLowerCase().toCharArray();
        boolean capitalizeNext = true;
        for (int i = 0; i < buffer.length; i++) {
            char ch = buffer[i];
            if (isDelimiter(ch, delimiters)) {
                capitalizeNext = true;
            } else if (capitalizeNext) {
                buffer[i] = toTitleCase(ch);
                capitalizeNext = false;
            }
        }
        return new String(buffer);
    }

    public static boolean isDelimiter(char ch, char[] delimiters) {
        if (delimiters == null) {
            return Character.isWhitespace(ch);
        }
        for (char delimiter : delimiters) {
            if (ch == delimiter) {
                return true;
            }
        }
        return false;
    }
}