Java Utililty Methods String Case Swap

List of utility methods to do String Case Swap

Description

The list of methods to do String Case Swap are organized into topic(s).

Method

StringswapCase(final String str)

Swaps the case of a String using a word based algorithm.

if (isEmpty(str)) {
    return str;
final char[] buffer = str.toCharArray();
boolean whitespace = true;
for (int i = 0; i < buffer.length; i++) {
    final char ch = buffer[i];
    if (Character.isUpperCase(ch)) {
...
StringswapCase(String str)

Swaps the case of String.

Properly looks after making sure the start of words are Titlecase and not Uppercase.

null is returned as null.

if (str == null) {
    return null;
int sz = str.length();
StringBuilder buffer = new StringBuilder(sz);
boolean whitespace = false;
char ch;
char tmp;
...
StringswapCase(String str)

Swaps the case of a String using a word based algorithm.

  • Upper case character converts to Lower case
  • Title case character converts to Lower case
  • Lower case character after Whitespace or at start converts to Title case
  • Other Lower case character converts to Upper case

Whitespace is defined by Character#isWhitespace(char) .

char[] buffer = str.toCharArray();
boolean whitespace = true;
for (int i = 0; i < buffer.length; i++) {
    char ch = buffer[i];
    if (Character.isUpperCase(ch)) {
        buffer[i] = Character.toLowerCase(ch);
        whitespace = false;
    } else if (Character.isTitleCase(ch)) {
...
StringswapCase(String str)
swap Case
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);
...
StringswapCase(String str)

Swaps the case of a String changing upper and title case to lower case, and lower case to upper case.

  • Upper case character converts to Lower case
  • Title case character converts to Lower case
  • Lower case character converts to Upper case

For a word based algorithm, see WordUtils#swapCase(String) .

int strLen;
if (str == null || (strLen = str.length()) == 0) {
    return str;
StringBuffer buffer = new StringBuffer(strLen);
char ch = 0;
for (int i = 0; i < strLen; i++) {
    ch = str.charAt(i);
...
StringswapCase(String str)
swap Case
int strLen;
if ((str == null) || ((strLen = str.length()) == 0)) {
    return str;
StringBuffer buffer = new StringBuffer(strLen);
char ch = '\000';
for (int i = 0; i < strLen; i++) {
    ch = str.charAt(i);
...
StringswapCase(String str)

Swaps the case of a String using a word based algorithm.

int strLen;
if (str == null || (strLen = str.length()) == 0) {
    return str;
StringBuilder buffer = new StringBuilder(strLen);
boolean whitespace = true;
char ch = 0;
char tmp = 0;
...
StringswapFirstLetterCase(String str)
convert first letter to a big letter or a small letter.
 StringUtil.trim('Password') = 'password' StringUtil.trim('password') = 'Password' 
StringBuffer sbuf = new StringBuffer(str);
sbuf.deleteCharAt(0);
if (Character.isLowerCase(str.substring(0, 1).toCharArray()[0])) {
    sbuf.insert(0, str.substring(0, 1).toUpperCase());
} else {
    sbuf.insert(0, str.substring(0, 1).toLowerCase());
return sbuf.toString();
...