swap String Case - Android java.lang

Android examples for java.lang:String Case

Description

swap String Case

Demo Code

public class Main {

  /**// www  . j a  va2 s . c o  m
   * 
   * <pre>
   * StringUtil.swapCase(null)  = null
   * StringUtil.swapCase("Han") = "hAN"
   * StringUtil.swapCase("hAn") = "HaN"
   * </pre>
   * 
   */
  public static String swapCase(String str) {
    if (str == null) {
      return null;
    }
    char[] charArray = str.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
      if (Character.isLowerCase(charArray[i])) {
        charArray[i] = Character.toUpperCase(charArray[i]);
      } else {
        charArray[i] = Character.toLowerCase(charArray[i]);
      }
    }

    return new String(charArray);
  }

}

Related Tutorials