Java String Title Case titleCaseTruncate(String s, int maxlen)

Here you can find the source of titleCaseTruncate(String s, int maxlen)

Description

title Case Truncate

License

Apache License

Declaration

public static String titleCaseTruncate(String s, int maxlen) 

Method Source Code

//package com.java2s;
/*/*from  ww  w. jav a  2  s.c  o  m*/
Copyright 2011 Karl-Michael Schneider
    
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 String titleCaseTruncate(String s, int maxlen) {
        return transform(s, true, false, maxlen);
    }

    public static String transform(String s, boolean titleCase, boolean makeIdentifier, int truncate) {
        char[] characters = s.toCharArray(); // transform string in-place
        int j = 0; // index into transformed string
        int length = 0; // length of transformed string
        boolean startWord = titleCase; // only relevant for title case
        for (int i = 0; i < characters.length;) {
            // cut off transformed string
            if (truncate > 0 && length >= truncate)
                break;

            // get next code point from source string
            int c = Character.codePointAt(characters, i);

            // transformed character
            int t = c;

            boolean isWordChar = titleCase && Character.isLetterOrDigit(c);
            boolean isIdentifierChar = makeIdentifier
                    && (i == 0 ? Character.isJavaIdentifierStart(c) : Character.isJavaIdentifierPart(c));

            if (isWordChar || isIdentifierChar) {
                // make first character in word uppercase
                if (startWord) {
                    t = Character.toUpperCase(t);
                    startWord = false;
                }
            } else {
                // next character starts a new word (if it is a word character)
                startWord = true;
            }

            // skip the character if making identifier and it is not an
            // identifier character
            if (!makeIdentifier || isIdentifierChar) {
                // copy character (deleted one or more characters previously)
                if (j < i) {
                    Character.toChars(t, characters, j);
                }

                // advance transformed string index
                j += Character.charCount(t);
            }

            // advance source string index
            i += Character.charCount(c);
        }
        return new String(characters, 0, j);
    }
}

Related

  1. titleCase(String s)
  2. titleCase(String str)
  3. titlecase(String str)
  4. titleCase(String txt)
  5. titleCasedName(T instance)
  6. toTitleCase(final int chr)
  7. toTitleCase(final String input)
  8. toTitleCase(final String inStr)
  9. toTitleCase(final String inStr, final boolean putRestInLC)