Solution for How to replace first word in flutter or dart
is Given Below:
for Example: “This is an Apple” replace “is” to “__”,
in java
public class HelloWorld {
public static void main(String []args) {
String a="This is an Apple";
String b=a.replaceFirst("\bis\b","__");
System.out.println(b);// This __ an Apple
}
}
but in flutter or dart \b is not working。
void main(){
String a="This is an Apple";
String b=a.replaceFirst("\bis\b","__");
print(b);//This is an Apple
}
I think you should use ‘\s’ instead of ‘\b’. See the example below
void main()
{
final originalString = 'This is an Apple';
final replacedString = originalString.replaceFirst(RegExp('\sis\s'), ' __ ');
print(replacedString); // This __ an Apple
}
try with this for single space
void main(){
String a="This is an Apple";
String b=a.replaceFirst(new RegExp(r"siss")," __ ");
print(b);//This __ an Apple
}
output:
This __ an Apple
Another method:
void main(){
String a="This is an Apple";
String b=a.replaceFirst(" is "," __ ");
print(b);//This __ an Apple
}