leftOf method

String leftOf(
  1. String char
)

Returns the left side of the String starting from char.

If char doesn't exist, null is returned.

Example

 String s = 'peanutbutter';
 String foo = s.leftOf('butter'); // returns 'peanut'

Implementation

String leftOf(String char) {
  if (this.isBlank) {
    return this;
  }

  int index = this.indexOf(char);
  if (index == -1) {
    throw Exception('Character not found');
  }

  return this.substring(0, index);
}