getEverythingBefore method

  1. @useResult
String getEverythingBefore(
  1. String find
)

Returns the substring before the first occurrence of find.

Returns the original string if find is not found. Safe for emoji and combining marks anywhere in the string.

Example:

'a-b-c'.getEverythingBefore('-');  // 'a'
'👋a-b'.getEverythingBefore('-');  // '👋a'

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String getEverythingBefore(String find) {
  final int atIndex = indexOf(find);

  return atIndex == -1 ? this : substring(0, atIndex);
}