Example usage for java.lang Character toLowerCase

List of usage examples for java.lang Character toLowerCase

Introduction

In this page you can find the example usage for java.lang Character toLowerCase.

Prototype

public static int toLowerCase(int codePoint) 

Source Link

Document

Converts the character (Unicode code point) argument to lowercase using case mapping information from the UnicodeData file.

Usage

From source file:com.cohort.util.String2.java

/** This changes the character's case to sentence case 
 * (first letter and first letter after each period capitalized).  This is simplistic.
 *///from   w w  w. j  a va 2s . c o m
public static String toSentenceCase(String s) {
    if (s == null)
        return null;
    int sLength = s.length();
    StringBuilder sb = new StringBuilder(s);
    boolean capNext = true;
    for (int i = 0; i < sLength; i++) {
        char c = sb.charAt(i);
        if (isLetter(c)) {
            if (capNext) {
                sb.setCharAt(i, Character.toUpperCase(c));
                capNext = false;
            } else {
                sb.setCharAt(i, Character.toLowerCase(c));
            }
        } else if (c == '.') {
            capNext = true;
        }
    }
    return sb.toString();
}

From source file:com.cohort.util.String2.java

/** This suggests a camel-case variable name.
 * /*from www . j a v  a  2s  . co  m*/
 * @param s the starting string for the variable name.
 * @return a valid variable name asciiLowerCaseLetter+asciiDigitLetter*, using camel case.
 *   This is a simplistic suggestion. Different strings may return the same variable name.
 *   null returns "null".
 *   "" returns "a".
 */
public static String toVariableName(String s) {
    if (s == null)
        return "null";
    int sLength = s.length();
    if (sLength == 0)
        return "a";
    s = modifyToBeASCII(s);
    s = toTitleCase(s);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < sLength; i++) {
        char c = s.charAt(i);
        if (isDigitLetter(c))
            sb.append(c);
    }
    if (sb.length() == 0)
        return "a";
    char c = sb.charAt(0);
    sb.setCharAt(0, Character.toLowerCase(c));
    if (c >= '0' && c <= '9')
        sb.insert(0, 'a');
    return sb.toString();
}

From source file:com.cohort.util.String2.java

/** 
 * This converts "camelCase99String" to "Camel Case 99 String"
 * //  w  w  w. j a  va2s  .c o  m
 * @param s the camel case string.
 * @return the string with spaces before capital letters.
 *   null returns null.
 *   "" returns "".
 */
public static String camelCaseToTitleCase(String s) {

    //change 
    //  but don't space out an acronym, e.g., E T O P O
    //  and don't split hyphenated words, e.g.,   Real-Time
    if (s == null)
        return null;
    int n = s.length();
    if (n <= 1)
        return s;
    StringBuilder sb = new StringBuilder(n + 10);
    sb.append(Character.toUpperCase(s.charAt(0)));
    for (int i = 1; i < n; i++) {
        char chi1 = s.charAt(i - 1);
        char chi = s.charAt(i);
        if (Character.isLetter(chi)) {
            if (chi1 == Character.toLowerCase(chi1) && Character.isLetterOrDigit(chi1)
                    && chi != Character.toLowerCase(chi))
                sb.append(' ');
        } else if (Character.isDigit(chi)) {
            if (chi1 == Character.toLowerCase(chi1) && Character.isLetter(chi1))
                sb.append(' ');
        }
        sb.append(chi);
    }
    return sb.toString();
}

From source file:com.rdm.common.util.StringUtils.java

/**
 * <p>Uncapitalizes a String changing the first letter to title case as
 * per {@link Character#toLowerCase(char)}. No other letters are changed.</p>
 *
 * <p>For a word based algorithm, see {@link com.rdm.common.util.text.WordUtils#uncapitalize(String)}.
 * A {@code null} input String returns {@code null}.</p>
 *
 * <pre>/*w  w w.  j a  va 2s .com*/
 * StringUtils.uncapitalize(null)  = null
 * StringUtils.uncapitalize("")    = ""
 * StringUtils.uncapitalize("Cat") = "cat"
 * StringUtils.uncapitalize("CAT") = "cAT"
 * </pre>
 *
 * @param str the String to uncapitalize, may be null
 * @return the uncapitalized String, {@code null} if null String input
 * @see com.rdm.common.util.text.WordUtils#uncapitalize(String)
 * @see #capitalize(String)
 * @since 2.0
 */
public static String uncapitalize(final String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }

    char firstChar = str.charAt(0);
    if (Character.isLowerCase(firstChar)) {
        // already uncapitalized
        return str;
    }

    return new StringBuilder(strLen).append(Character.toLowerCase(firstChar)).append(str.substring(1))
            .toString();
}

From source file:com.rdm.common.util.StringUtils.java

/**
 * <p>Swaps the case of a String changing upper and title case to
 * lower case, and lower case to upper case.</p>
 *
 * <ul>/*from w  w w. ja v  a2  s .c o m*/
 *  <li>Upper case character converts to Lower case</li>
 *  <li>Title case character converts to Lower case</li>
 *  <li>Lower case character converts to Upper case</li>
 * </ul>
 *
 * <p>For a word based algorithm, see {@link com.rdm.common.util.text.WordUtils#swapCase(String)}.
 * A {@code null} input String returns {@code null}.</p>
 *
 * <pre>
 * StringUtils.swapCase(null)                 = null
 * StringUtils.swapCase("")                   = ""
 * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
 * </pre>
 *
 * <p>NOTE: This method changed in Lang version 2.0.
 * It no longer performs a word based algorithm.
 * If you only use ASCII, you will notice no change.
 * That functionality is available in org.apache.commons.lang3.text.WordUtils.</p>
 *
 * @param str  the String to swap case, may be null
 * @return the changed String, {@code null} if null String input
 */
public static String swapCase(final String str) {
    if (StringUtils.isEmpty(str)) {
        return str;
    }

    final char[] buffer = str.toCharArray();

    for (int i = 0; i < buffer.length; i++) {
        final char ch = buffer[i];
        if (Character.isUpperCase(ch)) {
            buffer[i] = Character.toLowerCase(ch);
        } else if (Character.isTitleCase(ch)) {
            buffer[i] = Character.toLowerCase(ch);
        } else if (Character.isLowerCase(ch)) {
            buffer[i] = Character.toUpperCase(ch);
        }
    }
    return new String(buffer);
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  www.ja  v  a 2s . c  o m
 * Uncapitalizes a CharSequence changing the first letter to title case as
 * per {@link Character#toLowerCase(char)}. No other letters are changed.
 * </p>
 * 
 * <p>
 * For a word based algorithm, see
 * {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}. A
 * <code>null</code> input String returns <code>null</code>.
 * </p>
 * 
 * <pre>
 * uncapitalize(null)  = null
 * uncapitalize("")    = ""
 * uncapitalize("Cat") = "cat"
 * uncapitalize("CAT") = "cAT"
 * </pre>
 * 
 * @param cs
 *            the String to uncapitalize, may be null
 * @return the uncapitalized String, <code>null</code> if null String input
 * @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
 * @see #capitalize(CharSequence)
 * @since 2.0
 * @since 3.0 Changed signature from uncapitalize(String) to
 *        uncapitalize(CharSequence)
 */
public static String uncapitalize(CharSequence cs) {
    if (cs == null) {
        return null;
    }
    int strLen;
    if ((strLen = cs.length()) == 0) {
        return cs.toString();
    }
    return new StringBuilder(strLen).append(Character.toLowerCase(cs.charAt(0))).append(subSequence(cs, 1))
            .toString();
}

From source file:com.clark.func.Functions.java

/**
 * <p>//w ww  .  ja  v  a  2  s .  c  om
 * Swaps the case of a String changing upper and title case to lower case,
 * and lower case to upper case.
 * </p>
 * 
 * <ul>
 * <li>Upper case character converts to Lower case</li>
 * <li>Title case character converts to Lower case</li>
 * <li>Lower case character converts to Upper case</li>
 * </ul>
 * 
 * <p>
 * For a word based algorithm, see
 * {@link org.apache.commons.lang3.text.WordUtils#swapCase(String)}. A
 * <code>null</code> input String returns <code>null</code>.
 * </p>
 * 
 * <pre>
 * swapCase(null)                 = null
 * swapCase("")                   = ""
 * swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
 * </pre>
 * 
 * <p>
 * NOTE: This method changed in Lang version 2.0. It no longer performs a
 * word based algorithm. If you only use ASCII, you will notice no change.
 * That functionality is available in
 * org.apache.commons.lang3.text.WordUtils.
 * </p>
 * 
 * @param str
 *            the String to swap case, may be null
 * @return the changed String, <code>null</code> if null String input
 */
public static String swapCase(String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }
    StringBuilder buffer = new StringBuilder(strLen);

    char ch = 0;
    for (int i = 0; i < strLen; i++) {
        ch = str.charAt(i);
        if (Character.isUpperCase(ch)) {
            ch = Character.toLowerCase(ch);
        } else if (Character.isTitleCase(ch)) {
            ch = Character.toLowerCase(ch);
        } else if (Character.isLowerCase(ch)) {
            ch = Character.toUpperCase(ch);
        }
        buffer.append(ch);
    }
    return buffer.toString();
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Uncapitalizes a String, changing the first character to lower case as
 * per {@link Character#toLowerCase(int)}. No other characters are changed.</p>
 *
 * <p>For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}.
 * A {@code null} input String returns {@code null}.</p>
 *
 * <pre>//from w ww. ja  v a 2  s.  co  m
 * StringUtils.uncapitalize(null)  = null
 * StringUtils.uncapitalize("")    = ""
 * StringUtils.uncapitalize("cat") = "cat"
 * StringUtils.uncapitalize("Cat") = "cat"
 * StringUtils.uncapitalize("CAT") = "cAT"
 * </pre>
 *
 * @param str the String to uncapitalize, may be null
 * @return the uncapitalized String, {@code null} if null String input
 * @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
 * @see #capitalize(String)
 * @since 2.0
 */
public static String uncapitalize(final String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }

    final int firstCodepoint = str.codePointAt(0);
    final int newCodePoint = Character.toLowerCase(firstCodepoint);
    if (firstCodepoint == newCodePoint) {
        // already capitalized
        return str;
    }

    int newCodePoints[] = new int[strLen]; // cannot be longer than the char array
    int outOffset = 0;
    newCodePoints[outOffset++] = newCodePoint; // copy the first codepoint
    for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen;) {
        final int codepoint = str.codePointAt(inOffset);
        newCodePoints[outOffset++] = codepoint; // copy the remaining ones
        inOffset += Character.charCount(codepoint);
    }
    return new String(newCodePoints, 0, outOffset);
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Swaps the case of a String changing upper and title case to
 * lower case, and lower case to upper case.</p>
 *
 * <ul>/*from w  w  w.j  ava 2 s.  c  om*/
 *  <li>Upper case character converts to Lower case</li>
 *  <li>Title case character converts to Lower case</li>
 *  <li>Lower case character converts to Upper case</li>
 * </ul>
 *
 * <p>For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#swapCase(String)}.
 * A {@code null} input String returns {@code null}.</p>
 *
 * <pre>
 * StringUtils.swapCase(null)                 = null
 * StringUtils.swapCase("")                   = ""
 * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
 * </pre>
 *
 * <p>NOTE: This method changed in Lang version 2.0.
 * It no longer performs a word based algorithm.
 * If you only use ASCII, you will notice no change.
 * That functionality is available in org.apache.commons.lang3.text.WordUtils.</p>
 *
 * @param str  the String to swap case, may be null
 * @return the changed String, {@code null} if null String input
 */
public static String swapCase(final String str) {
    if (StringUtils.isEmpty(str)) {
        return str;
    }

    final int strLen = str.length();
    int newCodePoints[] = new int[strLen]; // cannot be longer than the char array
    int outOffset = 0;
    for (int i = 0; i < strLen;) {
        final int oldCodepoint = str.codePointAt(i);
        final int newCodePoint;
        if (Character.isUpperCase(oldCodepoint)) {
            newCodePoint = Character.toLowerCase(oldCodepoint);
        } else if (Character.isTitleCase(oldCodepoint)) {
            newCodePoint = Character.toLowerCase(oldCodepoint);
        } else if (Character.isLowerCase(oldCodepoint)) {
            newCodePoint = Character.toUpperCase(oldCodepoint);
        } else {
            newCodePoint = oldCodepoint;
        }
        newCodePoints[outOffset++] = newCodePoint;
        i += Character.charCount(newCodePoint);
    }
    return new String(newCodePoints, 0, outOffset);
}