Android examples for java.lang:String Case
get Capitalization Type for a string
/*/*from w ww. ja v a 2 s. com*/ * Copyright (C) 2012 The Android Open Source Project * * 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 { public static final int CAPITALIZE_NONE = 0; // No caps, or mixed case public static final int CAPITALIZE_FIRST = 1; // First only public static final int CAPITALIZE_ALL = 2; // All caps public static int getCapitalizationType(final String text) { // If the first char is not uppercase, then the word is either all lower case or // camel case, and in either case we return CAPITALIZE_NONE. final int len = text.length(); int index = 0; for (; index < len; index = text.offsetByCodePoints(index, 1)) { if (Character.isLetter(text.codePointAt(index))) { break; } } if (index == len) return CAPITALIZE_NONE; if (!Character.isUpperCase(text.codePointAt(index))) { return CAPITALIZE_NONE; } int capsCount = 1; int letterCount = 1; for (index = text.offsetByCodePoints(index, 1); index < len; index = text.offsetByCodePoints(index, 1)) { if (1 != capsCount && letterCount != capsCount) break; final int codePoint = text.codePointAt(index); if (Character.isUpperCase(codePoint)) { ++capsCount; ++letterCount; } else if (Character.isLetter(codePoint)) { // We need to discount non-letters since they may not be upper-case, but may // still be part of a word (e.g. single quote or dash, as in "IT'S" or // "FULL-TIME") ++letterCount; } } // We know the first char is upper case. So we want to test if either every // letter other // than the first is lower case, or if they are all upper case. If the string is // exactly // one char long, then we will arrive here with letterCount 1, and this is // correct, too. if (1 == capsCount) return CAPITALIZE_FIRST; return (letterCount == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE); } }