Hello friends, In today’s tutorial we would learn about finding and replacing special characters like ‘@#%^&*();’ and many more of them into a String in flutter. This all can be happen use of RegExp function which is a regular expression. We would use RegExp with String replaceAll() method. To understand how this would works ? First we have to understand what replaceAll() does ? String replaceAll function find and replace given expression to our given String or Character. Now when we would use RegExp and pass a fix argument inside it we would simply telling the replace function to remove all the given characters which is not string. I’ll explain them in the next steps. So in this tutorial we would learn about Find and Remove Special Characters From String in Flutter Dart.
Contents in this project Find and Remove Special Characters From 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 our main runApp() method, Here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main MyApp extends StatelessWidget class. Here we would call our main Child class.
1 2 3 4 5 6 |
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp(home: Scaffold(body: Center(child: Child()))); } } |
4. Creating Child class extends StatefulWidget widget. Here we would make our main class ChildWidget with createState() method.
1 2 3 |
class Child extends StatefulWidget { ChildWidget createState() => ChildWidget(); } |
5. Creating our main ChildWidget extends State<Child>.
1 2 3 4 5 |
class ChildWidget extends State<Child> { } |
6. Creating a String with some random characters. We would remove special characters from this string.
1 |
String tempData = 'hf^&*)(*&^%<>:"}:fdufjdghH--Hello-^%)Friends !@#%^&*()'; |
7. Creating a function named as removeSpecialCharacters(). In this function we would remove special characters from String the update the String using STATE.
NOTE :-
- You can use RegExp(‘[A-Za-z]’) to remove all the alphabets from string.
- Use RegExp(‘[0-9]’) to remove all the numbers from string.
- Use RegExp(‘[A-Za-z0-9]’) to remove all the alphabets and numbers from String.
1 2 3 4 5 6 |
void removeSpecialCharacters() { final output = tempData.replaceAll(RegExp('[^A-Za-z0-9]'), ''); setState(() { tempData = output; }); } |
8. Creating Widget Build area -> Make 1 Text and Button widget.
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 |
Widget build(BuildContext context) { return Scaffold( body: SafeArea( top: true, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( tempData, style: const TextStyle(fontSize: 24), textAlign: TextAlign.center, ), Container( margin: const EdgeInsets.all(8), child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Colors.green[600], padding: const EdgeInsets.all(6), textStyle: const TextStyle(fontSize: 20), ), child: const Text('Remove Special Characters From String'), onPressed: removeSpecialCharacters, ), ), ], ), ))); } |
9. 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp(home: Scaffold(body: Center(child: Child()))); } } class Child extends StatefulWidget { ChildWidget createState() => ChildWidget(); } class ChildWidget extends State<Child> { String tempData = 'hf^&*)(*&^%<>:"}:fdufjdghH--Hello-^%)Friends !@#%^&*()'; void removeSpecialCharacters() { final output = tempData.replaceAll(RegExp('[^A-Za-z0-9]'), ''); setState(() { tempData = output; }); } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( top: true, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( tempData, style: const TextStyle(fontSize: 24), textAlign: TextAlign.center, ), Container( margin: const EdgeInsets.all(8), child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Colors.green[600], padding: const EdgeInsets.all(6), textStyle: const TextStyle(fontSize: 20), ), child: const Text('Remove Special Characters From String'), onPressed: removeSpecialCharacters, ), ), ], ), ))); } } |
Screenshots :-