Hello friends, In today’s tutorial we would learn about a Dart string replacing function. In flutter Using the String.replaceAll() method we can find and replace substring of string in flutter dart, replace single string character or words present in a single document. In this tutorial we are calling the String.replaceAll() method on button click event so all the changes on the string will be on application runtime. So let’s get started 🙂 .
Contents in this project Example of Find and Replace Substring of String in Flutter Dart:-
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Creating void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp extends StatelessWidget class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating a String variable named as message with some random string. We would replace the website word in given string.
1 |
String message = "Welcome To Our Website Flutter-Examples.com"; |
5. Creating a function named as changeData(). In this function we would use the String.replaceAll() method to find and replace Website word word with Blog word in given string. After changing we would store the result in temp string variable and then print the temp on Terminal screen.
1 2 3 4 5 |
void changeData() { String temp = message.replaceAll("Website", "Blog"); print(temp); } |
6. Creating Widget Build Area -> Scaffold widget -> Material App -> Scaffold widget -> Container Widget -> Center Widget -> Raised Button widget. We would call the changeData function on button onPress event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => changeData(), child: Text( ' Click Here To Find & Replace String in Flutter Dart', textAlign: TextAlign.center, style: TextStyle(fontSize: 26), ), textColor: Colors.white, color: Colors.purpleAccent, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ))); } |
7. Complete source code for main.dart file:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { String message = "Welcome To Our Website Flutter-Examples.com"; void changeData() { String temp = message.replaceAll("Website", "Blog"); print(temp); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => changeData(), child: Text( ' Click Here To Find & Replace String in Flutter Dart', textAlign: TextAlign.center, style: TextStyle(fontSize: 26), ), textColor: Colors.white, color: Colors.purpleAccent, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), )), ))); } } |
Screenshots:-
