Java String Sentence Case toSentenceCase(String inputString)

Here you can find the source of toSentenceCase(String inputString)

Description

BETA VERSION: converts an URL to UpperCamelCase TODO: test the correct behavior

License

Apache License

Declaration

public static String toSentenceCase(String inputString) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w  w w  .java  2  s  . co m
     * BETA VERSION: converts an URL to UpperCamelCase
     * TODO: test the correct behavior
     */
    public static String toSentenceCase(String inputString) {
        String result = "";
        if (inputString.length() == 0) {
            return result;
        }
        char firstChar = inputString.charAt(0);
        char firstCharToUpperCase = Character.toUpperCase(firstChar);
        result = result + firstCharToUpperCase;
        boolean terminalCharacterEncountered = false;
        char[] terminalCharacters = { '.', '?', '!', '_', ' ', '-' };
        for (int i = 1; i < inputString.length(); i++) {
            char currentChar = inputString.charAt(i);
            if (terminalCharacterEncountered) {
                if (currentChar == ' ') {
                    result = result + currentChar;
                } else {
                    char currentCharToUpperCase = Character.toUpperCase(currentChar);
                    result = result + currentCharToUpperCase;
                    terminalCharacterEncountered = false;
                }
            } else {
                char currentCharToLowerCase = Character.toLowerCase(currentChar);
                result = result + currentCharToLowerCase;
            }
            for (int j = 0; j < terminalCharacters.length; j++) {
                if (currentChar == terminalCharacters[j]) {
                    terminalCharacterEncountered = true;
                    break;
                }
            }
        }
        result = result.replaceAll("-", "");
        return result;
    }
}

Related

  1. toSentenceCase(final String s)
  2. toSentenceCase(String original)
  3. toSentenceCase(String s, char endOfLineSym)
  4. toSentenceCase(String str)
  5. toSentenceCase(String string)