Home > Blockchain >  How to remove last element from a list in dart?
How to remove last element from a list in dart?

Time:06-28

I'm a beginner in dart.

void main() {
  var abf = ' 37.4054-122.0999/';
  var abf2;
  abf2 = abf.replaceAll(" "," ");
  var abf1 = abf2.split(RegExp('(?=[ -])'));
  print (abf1[0]);
  print (abf1[1]);
}

The above code splits abf into two values for me I want to remove the ending '/'. I tried many split methods using other variables but it's not removing the '/' even though its removing the ' '.

CodePudding user response:

It's not really clear what you're trying to do with the split.

But if you're looking the remove the / this should work:

String number = ' 37.4054-122.0999/';
number = number.replaceAll("/"," ");

CodePudding user response:

You can create substring from this while you like to remove last element.

  String abf = ' 37.4054-122.0999/';

  final result = abf.substring(0, abf.length - 1);
  print(result);

CodePudding user response:

Dart's List class has a built-in removeLast method. Maybe you can try to split the string and then removing the last element:

String str = "str";
String newStr = str.split(''). removeLast().join('');
  •  Tags:  
  • dart
  • Related