In Flutter App development where we use dart language, we can replace or remove substrings from the original string using dart In-Built String Methods:
- replaceAll()
- replaceAllMapped()
- replaceFirst()
- replaceFirstMapped()
- replaceRange()
Now, let’s use each of this dart string method to replace string.
Dart replaceAll() method
In below Example, We are use replaceAll() function, so what this will do is it will replace all the occurences of word/substring “easy” with “difficult” in the given string.
void main() { String str = "This homework is very easy"; String substr = "easy"; String replace = "difficult"; String newStr = str.replaceAll(substr, replace); print(newStr); }
Output of above dart code
This homework is very difficult
Dart replaceRange() method
In below Example, I want to replace range of index from the given string. From example I have a String sentence “The bird is sitting on the branch, The bird is Looking Beautiful”. here I want to replace “bird” with “sparrow” by using range/index of string.
the replaceRange() method accepts 3 parameter replaceRange(startIndex , EndIndex, replacement string) as shown below:
void main() { String str = "The bird is sitting on the branch, The bird is Looking Beautiful"; String newStr = str.replaceRange(4, 8, "sparrow"); print(newStr); }
Output of above dart code
The sparrow is sitting on the branch, The bird is Looking Beautiful
Dart replaceAllMapped() method
Suppose I want to replace all the words that has 8 letters in the given string, then at this time I can make use of replaceAllMapped() function.
void main() { String str = "I Like to play fortnite game and Learn Programming from Proto Coders Point"; // Here I am using Regular Expression to match 8 letter word in given string // and then replace it with _________ var pattern = RegExp(r'\b\w{8}\b'); String replacement = "________"; String newStr = str.replaceAllMapped(pattern, (match) => replacement); print(newStr); }
In Above code I am using Regular Expression to match 8 letter word in given string and then replace it with _____.
Output of above dart code
I Like to play ________ game and Learn Programming from Proto Coders Point
Dart replaceFirst() Method
Suppose you want to replace only the first word that occurs in the given string, Let’s say I have sentence “don’t have a bad day have a great day, because bad day will come and go”. Here I want to only change first occurrence of word bad, In such situation I can make use of dart replaceFirst() methon.
void main() { String str = "don't have a bad day have a great day"; String substr = "bad"; String replacement = "good"; String newStr = str.replaceFirst(substr, replacement); print(newStr); }
Output of above dart code
don't have a good day have a great day
Dart replaceFirstMapped() method
In below example, I have a sentence “I Like to play fortnite game, fortnite is my favourite game and Learn Programming from Proto Coders Point”, which contain 8 letter word “fortnite” for 2 times, I want to replace only the first occurrences of it.
void main() { String str = "I Like to play fortnite game, fortnite is my favourite game and Learn Programming from Proto Coders Point"; // Here I am using Regular Expression to match 8 letter word in given string // and then replace it with _________ var pattern = RegExp(r'\b\w{8}\b'); String replacement = "________"; String newStr = str.replaceFirstMapped(pattern, (match) => replacement); print(newStr); }
Output of above dart code
I Like to play ________ game, fortnite is my favourite game and Learn Programming from Proto Coders Point
To remove any word from a given string then simple pass an empty replacement (“”)
str.replaceAll("bad",""); // by doing this bad word from a given string will get removed.