Home > Mobile >  How to remove a string date exist in a list of Strings in dart?
How to remove a string date exist in a list of Strings in dart?

Time:06-22

I have a list of strings and one if it's items looks like this '19-06-2022 12:37' ..

How can i remove any pattern like this if it exist in the list using Dart language ?

CodePudding user response:

You can remove any pattern from string by using String class method replaceAll. If you want to remove any pattern then you need to use RegExp like below example.

There are also other method like replaceFirst, replaceRange for more details Read Here

 String testSt = "12-02-2022 16:23";

 String myPattern = "^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2}) [012]{0,1}[0-9]:[0-6][0-9]";

 testSt.replaceAll(new RegExp(myPattern),"");

CodePudding user response:

You may try out this with regex

void main() {
  RegExp regexp = RegExp(r'\d{2}-\d{2}-\d{4}\s\d{2}:\d{2}');

  String data = "others data 19-06-2022 12:37 other iunfo";
  print(regexp.allMatches(data).map((z) => z.group(0)).toList()); // [19-06-2022 12:37]
}

check out there demo

CodePudding user response:

You can try like this:

  List<String> stringList = ["20-11-2022 12:37", "19-06-2022 12:37", "19-03-2022 12:37", "09-06-2022 12:37", "x", "y", "z","w"];
  List<String> tempList = [];

     for(var i = 0; i < stringList.length; i  ){
       if(stringList[i].contains("-")){
        tempList.add(stringList[i]);
       }
     }

     tempList.forEach((element) {stringList.remove(element);});
  • Related