← All languages
Dart function of the day
Random

startsWith

Check whether a string starts with a given pattern.

Description

The startsWith method returns true if the string begins with the specified pattern at the given index. The pattern can be a String or a RegExp. An optional index parameter specifies the position in the string from which to check.

This is commonly used for prefix checking, routing logic, or filtering collections of strings. When the index parameter is provided, the method checks whether the pattern matches at that specific position rather than at the beginning of the string.

The check is case-sensitive. For case-insensitive prefix matching, convert both the string and the pattern to the same case using toLowerCase or toUpperCase before comparing.

Arguments

NameDescriptionOptional
pattern The Pattern (String or RegExp) to check at the start. No
index The index position to start checking from. Defaults to 0. Yes

Example

print('Dart is great'.startsWith('Dart')); // true
print('Dart is great'.startsWith('great')); // false
print('Dart is great'.startsWith('is', 5)); // true

Reference