before method

String before(
  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.getBefore('butter'); // returns 'peanut'

Implementation

String before(String char) {
  if (isBlank) {
    return blankIfNull;
  }

  int index = indexOf(char);
  if (index == -1) {
    return "";
  }

  return substring(0, index);
}