takeFirst method

String takeFirst(
  1. int count
)

Returns the first count characters of the string.

Returns an empty string if count is negative, or the entire string if count exceeds the string length.

Example:

'hello'.takeFirst(2)    // 'he'
'hi'.takeFirst(5)       // 'hi'
'abc'.takeFirst(0)      // ''

Implementation

String takeFirst(int count) {
  if (count < 0) { return ''; }
  if (count > length) { return this; }
  return substring(0, count);
}