startsWith method

bool startsWith(
  1. Pattern pattern, [
  2. int index = 0
])

Whether this string starts with a match of pattern.

const string = 'Dart is open source';
print(string.startsWith('Dar')); // true
print(string.startsWith(RegExp(r'[A-Z][a-z]'))); // true

If index is provided, this method checks if the substring starting at that index starts with a match of pattern:

const string = 'Dart';
print(string.startsWith('art', 0)); // false
print(string.startsWith('art', 1)); // true
print(string.startsWith(RegExp(r'\w{3}'), 2)); // false

index must not be negative or greater than length.

A RegExp containing '^' does not match if the index is greater than zero and the regexp is not multi-line. The pattern works on the string as a whole, and does not extract a substring starting at index first:

const string = 'Dart';
print(string.startsWith(RegExp(r'^art'), 1)); // false
print(string.startsWith(RegExp(r'art'), 1)); // true

Implementation

bool startsWith(Pattern pattern, [int index = 0]) =>
    value.startsWith(pattern, index);