Replace All Occurrences of a Substring in Dart
Use the Dart String.replaceAll() method to replace every occurrence of a substring or pattern with another string. The method returns a new string and does not modify the original string.
Dart String.replaceAll() Syntax
The syntax of replaceAll() method is:
String.replaceAll(Pattern pattern, String newSubString)
The pattern argument identifies the text to replace. It can be a plain String or another object implementing Pattern, such as RegExp. The newSubString argument contains the replacement text.
pattern: the substring or regular-expression pattern to find.newSubString: the string inserted in place of each match.- Return value: a new
Stringcontaining the replacements.
String matching is case-sensitive. For example, replacing 'Hello' does not replace 'hello'.
Replace a Substring with Another String
In this example, the substring 'Hello' is replaced with 'Hi'. Because replaceAll() replaces every match, both occurrences are changed.
Dart Program
void main(){
String str = 'Hello TutorialKart. Hello User.';
//replace subString
String result = str.replaceAll('Hello', 'Hi');
print(result);
}
Output
Hi TutorialKart. Hi User.
The value assigned to str remains unchanged. The replaced text is available in the new result string.
What Happens When the Substring Is Not Found?
In the following program, the source string does not contain 'Ola'. Therefore, no characters are replaced.
Dart Program
void main(){
String str = 'Hello TutorialKart. Hello User.';
//replace subString
String result = str.replaceAll('Ola', 'Hi');
print(result);
}
Output
Hello TutorialKart. Hello User.
When the pattern has no match, replaceAll() returns a string with the same text as the original.
Chain Multiple replaceAll() Calls
You can chain replaceAll() calls when different substrings must be replaced. Each call operates on the string returned by the previous call.
Dart Program
void main(){
String str = 'Hello TutorialKart. Hello User.';
//replaceAll() chaining
String result = str.replaceAll('Hello', 'Hi').replaceAll('User', 'Client');
print(result);
}
Output
Hi TutorialKart. Hi Client.
The first call replaces 'Hello' with 'Hi'. The second call then replaces 'User' with 'Client' in the result of the first operation.
Replace a Substring Without Case Sensitivity
A plain string pattern is case-sensitive. To replace uppercase and lowercase variations of the same text, pass a RegExp with caseSensitive: false.
void main() {
String text = 'Dart is useful. I am learning DART.';
String result = text.replaceAll(
RegExp('dart', caseSensitive: false),
'Flutter',
);
print(result);
}
Output
Flutter is useful. I am learning Flutter.
Replace Text Matching a Regular Expression
Use a RegExp pattern when the text to replace follows a rule rather than matching one fixed substring. The following example replaces every sequence of digits with '#'.
void main() {
String text = 'Order 125 contains 3 items.';
String result = text.replaceAll(RegExp(r'\d+'), '#');
print(result);
}
Output
Order # contains # items.
The raw string prefix r keeps the regular-expression backslash from being interpreted as a Dart string escape.
Remove a Substring from a Dart String
To remove every occurrence of a substring, replace it with an empty string.
void main() {
String text = 'red-green-red';
String result = text.replaceAll('red', '');
print(result);
}
Output
-green-
Only the matched substring is removed. Other characters, including separators around the matches, remain in the result.
Replace Only the First Matching Substring
replaceAll() replaces every match. Use replaceFirst() when only the first occurrence should be changed.
void main() {
String text = 'one two one two';
print(text.replaceAll('one', '1'));
print(text.replaceFirst('one', '1'));
}
Output
1 two 1 two
1 two one two
Common Details When Replacing Dart Substrings
- Dart strings are immutable, so save or use the value returned by
replaceAll(). - Plain string patterns are matched literally and are case-sensitive.
- An empty replacement string removes each matching substring.
- Use
RegExpfor case-insensitive replacements or pattern-based matching. - Use
replaceFirst()instead ofreplaceAll()when only one match should be replaced.
Frequently Asked Questions About Dart replaceAll()
Does replaceAll() modify the original Dart string?
No. Dart strings are immutable. replaceAll() returns a new string containing the replacements, while the original string remains unchanged.
Is Dart replaceAll() case-sensitive?
Yes, when a plain string is used as the pattern. For case-insensitive replacement, use RegExp('text', caseSensitive: false).
How do I remove all occurrences of a substring in Dart?
Pass an empty string as the replacement value, such as text.replaceAll('old', '').
What is the difference between replaceAll() and replaceFirst()?
replaceAll() replaces every matching occurrence. replaceFirst() replaces only the first match, or the first match at or after an optional starting index.
Summary of Replacing Substrings in Dart
Use String.replaceAll() to replace all occurrences of a substring or pattern in a Dart string. Pass a plain string for literal, case-sensitive matching or a RegExp for pattern-based and case-insensitive replacement. To replace only one occurrence, use replaceFirst(). Continue with the Dart Tutorial for more Dart string operations and examples.
TutorialKart.com