Introduction :
In this tutorial, we will learn how to replace a part of a string in dart. Dart string class comes with a method called replaceRange, that we can use to replace a part of the string with a different string. In this tutorial, we will learn how to use replaceRange with an example in Dart.
Definition :
replaceRange is defined as below :
String replaceRange (
int start,
int end,
String str
)
It replaces the string from index start to end with different string str. It returns one new string i.e. the modified string.
The start and end indices should be in a valid range. If the value of end is null, it will take the default length length.
Example :
import "dart:core";
void main() {
final givenString = "hello world";
final newString = givenString.replaceRange(0, 3, "1");
print(newString);
}
It will print the below output :
1lo world
As you can see in the above example, it replaced the string part from index 0 to 3 with 1.
Try to run the above example with different indices and different strings to learn more about how replaceRange works.