VOOZH about

URL: https://www.geeksforgeeks.org/dart/how-to-replace-a-substring-of-a-string-in-dart/

⇱ How to Replace a Substring of a String in Dart? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Replace a Substring of a String in Dart?

Last Updated : 7 Apr, 2025

To replace all the substrings of a string, we make use of the replaceAll method in Dart. This method replaces all the substrings in the given string with the desired substring. Returns a new string in which the non-overlapping substrings matching from (the ones iterated by from.allMatches(this String)) are replaced by the literal string replace.

Syntax:

String_name.replaceAll ( Old_Sub_String , New_Sub_String );

In the above sequence:

  • String_name is the name of the string in which the operation is to be done. It could be the string itself.
  • Old_String_Pattern is the pattern present in the given string.
  • New_String_Pattern is the new pattern that is to be replaced with the old pattern.

Image Representation:

👁 Replacing_string


Example 1: 

Replacing the substring in the given string.


Output:

Welcome Geek!

In the above example, the substring "GeeksForGeeks" is replaced by another string "Geek!".


Example 2:

Using chain of replaceAll() method to change the string in dart.


Output:

Welcome Geek :)

In the above example, the substring "GeeksForGeeks" is replaced by another string "Geek!" and then "!" is replaced by " : )".

Comment
Article Tags:
Article Tags:

Explore