Find and Remove Special Characters From String in Flutter Dart

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.

2. Creating our main runApp() method, Here we would call our main MyApp class.

3. Creating our main MyApp extends StatelessWidget class. Here we would call our main Child class.

4. Creating Child class extends StatefulWidget widget. Here we would make our main class ChildWidget with createState() method.

5. Creating our main ChildWidget extends State<Child>.

6. Creating a String with some random characters. We would remove special characters from this string.

7. Creating a function named as removeSpecialCharacters(). In this function we would remove special characters from String the update the String using STATE.

NOTE :-

  1. You can use RegExp(‘[A-Za-z]’) to remove all the alphabets from string.
  2. Use RegExp(‘[0-9]’) to remove all the numbers from string.
  3. Use RegExp(‘[A-Za-z0-9]’) to remove all the alphabets and numbers from String.

8. Creating Widget Build area -> Make 1 Text and Button widget.

9. Complete source code for main.dart file :-

Screenshots :-

Leave a Reply

Your email address will not be published. Required fields are marked *