Java String Capitalize Word capitalizedWords(String s)

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

Description

capitalized Words

License

Open Source License

Declaration

public static final boolean capitalizedWords(String s) 

Method Source Code

//package com.java2s;
/*//from w  w w . ja va2  s  . c om
  Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
  This file is part of the Semantic Discovery Toolkit.
    
  The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
    
  The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.
    
  You should have received a copy of the GNU Lesser General Public License
  along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */

public class Main {
    public static final boolean capitalizedWords(String s) {
        boolean capitalized = true;

        final int len = s.length();
        int wordPos = 0;

        while (wordPos < len) {
            final int cp = s.codePointAt(wordPos);
            if (Character.isUpperCase(cp) || Character.isDigit(cp)) {
                // have a capital where we expect it. need to find a lowercase before the next position.
                final int nextWordPos = nextWordPos(s, wordPos);
                boolean foundLower = false;
                for (int i = wordPos + 1; i < nextWordPos; ++i) {
                    if (Character.isLowerCase(s.codePointAt(i))) {
                        foundLower = true;
                    }
                }
                if (!foundLower) {
                    capitalized = false;
                    break;
                }
                wordPos = nextWordPos;
            } else {
                capitalized = false;
                break;
            }
        }

        return capitalized;
    }

    public static final int nextWordPos(String s, int curWordPos) {
        final int len = s.length();
        int result = len;

        // look for a space, then spin beyond all non-chars.
        int endOfWord = s.indexOf(' ', curWordPos);
        if (endOfWord >= curWordPos) {
            // found a space, now spin to first char
            for (result = endOfWord + 1; result < len; ++result) {
                if (Character.isLetterOrDigit(s.codePointAt(result))) {
                    break;
                }
            }
        }

        return result;
    }
}

Related

  1. capitalizedWord(String s)
  2. CapitalizeEachWord(String Str)
  3. capitalizeEachWord(String string)
  4. capitalizeFirstCharWords(String sentence)
  5. capitalizeFirstLetter(final String word)