Dart String substring() Method
Use the Dart String.substring() method to extract part of a string. Pass the index where the substring should begin and, optionally, the index where it should end.
The start index is included in the result, while the end index is excluded. Dart string indexes are zero-based, so the first character is at index 0.
Dart substring() Syntax
The syntax of the String.substring() method is:
String.substring(int startIndex, [ int endIndex ])
startIndexspecifies the first character to include.endIndexspecifies the first character not to include.- If
endIndexis omitted, Dart extracts characters fromstartIndexto the end of the string.
Therefore, substring(startIndex, endIndex) extracts the range [startIndex, endIndex).
Valid Index Rules for Dart substring()
The indexes supplied to substring() must satisfy all of the following conditions:
startIndexmust not be negative.endIndexmust not be greater than the string’slength.startIndexmust not be greater thanendIndex.- An index equal to
lengthis valid and represents the position immediately after the final character.
Dart throws a RangeError when an index is outside the valid range.
Dart Substring Examples
Extract a Substring with Start and End Indexes
In this example, the substring starts at index 5 and ends immediately before index 13.
main.dart
void main(){
String str = 'HelloTutorialKart.';
int startIndex = 5;
int endIndex = 13;
//find substring
String result = str.substring(startIndex, endIndex);
print(result);
}
Output
Tutorial
The index range of the given string is:
H e l l o T u t o r i a l K a r t .
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
^**************************** ^
startingIndex EndingIndex
The character at index 5, T, is included. The character at index 13, K, is excluded.
Extract a Substring without an End Index
When the ending index is omitted, substring() returns every character from the starting index through the end of the string.
main.dart
void main(){
String str = 'HelloTutorialKart.';
int startIndex = 5;
//find substring
String result = str.substring(startIndex);
print(result);
}
Output
TutorialKart.
The index range of the given string is:
H e l l o T u t o r i a l K a r t .
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
^************************************************
startingIndex
Extract the First Characters of a Dart String
Use 0 as the starting index to extract characters from the beginning of a string. The following example gets the first five characters:
void main() {
String text = 'Dart Programming';
String firstWord = text.substring(0, 4);
print(firstWord);
}
Output
Dart
The end index is 4, but the character at index 4 is not included.
Extract Text after a Known Separator
You can combine indexOf() with substring() when the required position depends on a separator or another known character.
void main() {
String email = 'alex@example.com';
int separatorIndex = email.indexOf('@');
String domain = email.substring(separatorIndex + 1);
print(domain);
}
Output
example.com
Adding 1 skips the @ character itself.
Return an Empty String with Equal Indexes
If the start and end indexes are equal, the selected range contains no characters, so the result is an empty string.
void main() {
String text = 'Dart';
String result = text.substring(2, 2);
print(result.isEmpty);
}
Output
true
Avoid RangeError When Extracting a Dart Substring
Validate calculated indexes before passing them to substring(). This is particularly important when an index comes from user input or from a method such as indexOf(), which returns -1 when no match is found.
void main() {
String value = 'name=value';
int separatorIndex = value.indexOf('=');
if (separatorIndex != -1) {
String result = value.substring(separatorIndex + 1);
print(result);
} else {
print('Separator not found');
}
}
Output
value
Dart substring() and Unicode Characters
Dart string indexes operate on UTF-16 code units rather than user-perceived characters. Many common letters occupy one code unit, but some emoji and combined Unicode symbols occupy multiple code units. Splitting such a sequence at an arbitrary index can produce an incomplete character.
For ordinary ASCII text, identifiers, file extensions, and predictable separators, substring() is generally straightforward. Applications that must safely segment emoji or complex writing systems should work with Unicode grapheme clusters rather than assuming that every visible character has a length of one.
Dart substring() Frequently Asked Questions
Is the end index included in a Dart substring?
No. The character at startIndex is included, but the character at endIndex is excluded.
How do I get a substring to the end of a Dart string?
Pass only the starting index. For example, text.substring(3) returns the part of text beginning at index 3.
What happens when startIndex equals endIndex?
The method returns an empty string because there are no characters between the two equal boundaries.
Why does Dart substring() throw a RangeError?
A RangeError occurs when an index is negative, an index exceeds the string length, or the start index is greater than the end index.
Summary of Dart String Substring Extraction
Use substring(startIndex, endIndex) to extract a range whose start is included and whose end is excluded. Omit endIndex to extract the rest of the string, and validate dynamically calculated indexes to prevent a RangeError.
In this Dart Tutorial, we learned how to extract a substring using a starting index and an optional ending index.
TutorialKart.com