TransWikia.com

Flutter/Dart - Regex for removing hashtag and spaces?

Stack Overflow Asked on December 25, 2021

The user is asked to fill in a field with a tag containing no hashtags and no spaces. But some will no doubt do it anyway. How do I strip the hashtags and spaces before sending it off to the database? Here’s the code I used to try and simply remove the hashtag. But although it prints the correct removal in the console in realtime as I type into the field, I get the following error when I try to post it off to the server:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s) (input): Must not be null

So here’s the code;

     child: TextField(
                    keyboardType: TextInputType.text,
                    autocorrect: false,
                    onChanged: (tag1text){
                      nohash1 = tag1text.replaceAll('#', '');
                      print("This is nohash1 " + nohash1);
                      setState(() {
                        this.tag1 = nohash1;
                      });
                    },
                  ), 

One Answer

You can try .replaceAll(RegExp('[# ]'),'') . Your original code only removed matching #. Using RegExp('[# ]') we can specify the regex pattern to use for removal.

void main() {
  String inputText = '#big dog sled';
  print(inputText.replaceAll(RegExp('[# ]'),''));
}

Output :

bigdogsled

Answered by dev-aentgs on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP