The syntax of String.replaceAll() method is:
String replaceAll(Pattern from, String replace)
This replaceAll() method of String replaces all substrings that match from
with replace
.
Parameter | Optional/Required | Description |
---|---|---|
from | required | the substring or pattern to search for within the string |
replace | required | the string that replaces each substring or pattern found |
String.replaceAll() returns value of type String
.
In this example,
str1
with the value 'Hello, world!'.replaceAll()
method with a regular expression RegExp('[aeiou]')
to match vowels.str1
.void main() {
String str1 = 'Hello, world!';
String replacedStr1 = str1.replaceAll(RegExp('[aeiou]'), '*');
print('Replaced string: $replacedStr1');
}
Replaced string: H*ll*, w*rld!
In this example,
str2
with the value 'ABCDE'.replaceAll()
method to replace occurrences of 'D' with an asterisk '*' in str2
.void main() {
String str2 = 'ABCDE';
String replacedStr2 = str2.replaceAll('D', '*');
print('Replaced string: $replacedStr2');
}
Replaced string: ABC*E
In this example,
str3
with the value 'Lorem ipsum dolor sit amet'.replaceAll()
method to replace all occurrences of 'ipsum' with 'replacement' in str3
.void main() {
String str3 = 'Lorem ipsum dolor sit amet';
String replacedStr3 = str3.replaceAll('ipsum', 'replacement');
print('Replaced string: $replacedStr3');
}
Replaced string: Lorem replacement dolor sit amet
In this Dart tutorial, we learned about replaceAll() method of String: the syntax and few working examples with output and detailed explanation for each example.