swapCase method

String swapCase()

Swaps the case in the String.

Example

String foo = 'Hello World';
String swapped = foo.swapCase(); // returns 'hELLO wORLD';

Implementation

String swapCase() {
  if (this.isBlank) {
    return this;
  }

  List<String> letters = this.toArray;

  String swapped = '';

  for (final l in letters) {
    if (l.isUpperCase) {
      swapped += l.toLowerCase();
    } else {
      swapped += l.toUpperCase();
    }
  }
  return swapped;
}