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) {
    return null;
  }

  return this!.substring(0, index);
}