Java String Capitalize Word capitalizedWord(String s)

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

Description

capitalized Word

License

Open Source License

Declaration

public static final boolean capitalizedWord(String s) 

Method Source Code

//package com.java2s;
/*//from w ww  .  ja va2  s  .com
  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 capitalizedWord(String s) {
        return capitalizedWord(s, 0, s.length());
    }

    public static final boolean capitalizedWord(String s, int startPos, int endPos) {
        boolean capitalized = false;
        int c = s.codePointAt(startPos++);
        if (c > 65535)
            ++startPos;
        if (isCapital(c)) {
            capitalized = allLowerCase(s, startPos, endPos) && isWord(s, startPos, endPos);
        }
        return capitalized;
    }

    public static final boolean isCapital(String s, int pos) {
        return Character.isUpperCase(s.codePointAt(pos));
    }

    public static final boolean isCapital(int codePoint) {
        //Character.isLetter(c) && (Character.isUpperCase(c) || Character.isTitleCase(c)))
        return Character.isUpperCase(codePoint);
    }

    public static final boolean allLowerCase(String s) {
        return allLowerCase(s, 0, s.length());
    }

    /**
       * @return true if all letters in the string are lower case and there is at least one letter.
       */
    public static final boolean allLowerCase(String s, int startPos, int endPos) {
        boolean allLower = true;
        boolean foundOne = false;
        for (int i = startPos; i < endPos; ++i) {
            final int c = s.codePointAt(i);
            if (c > 65535)
                ++i;
            if (Character.isLetter(c)) {
                foundOne = true;
                if (!Character.isLowerCase(c)) {
                    allLower = false;
                    break;
                }
            }
        }
        return allLower && foundOne;
    }

    public static final boolean isWord(String s) {
        return isWord(s, 0, s.length());
    }

    public static final boolean isWord(String s, int startPos, int endPos) {
        boolean isWord = true;
        boolean foundOne = false;
        for (int i = startPos; i < endPos; ++i) {
            final int c = s.codePointAt(i);
            if (c > 65535)
                ++i;
            if (!Character.isLetter(c)) {
                isWord = false;
                break;
            } else {
                if (!foundOne)
                    foundOne = true;
            }
        }
        return isWord && foundOne;
    }
}

Related

  1. capitalizedWords(String s)
  2. CapitalizeEachWord(String Str)
  3. capitalizeEachWord(String string)
  4. capitalizeFirstCharWords(String sentence)