Java String Case Swap swapCase(String str)

Here you can find the source of swapCase(String str)

Description

swap Case

License

Apache License

Declaration

public static String swapCase(String str) 

Method Source Code

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

public class Main {

    public static String swapCase(String str) {
        int strLen;

        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }//from  ww w.  j  a  v  a2s . c om

        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();
    }

    public static String toLowerCase(String str) {
        if (str == null) {
            return null;
        }

        return str.toLowerCase();
    }

    public static String toUpperCase(String str) {
        if (str == null) {
            return null;
        }

        return str.toUpperCase();
    }

    /**
     * if <code>obj</code> is null, returns empty string. otherwise returns
     * <code>obj.toString()</code>
     * 
     * @param obj
     *           any ref you want
     * @return <code>obj</code> or <code>obj.toString()</code>
     */
    public static String toString(Object obj) {
        return obj == null ? "" : obj.toString();
    }
}

Related

  1. swapCase(final String str)
  2. swapCase(String str)
  3. swapCase(String str)
  4. swapCase(String str)
  5. swapCase(String str)
  6. swapCase(String str)
  7. swapFirstLetterCase(String str)