Breaking News
Breaking News

Reputation: 420

How to remove last letter of string from list? Dart

Hi guys I'm trying to remove or hide the last letter from List

Any possible ways?

Text(list[i].name,
          style: GoogleFonts.poppins(
              fontWeight: FontWeight.w400,
              color:
              list[i].isBook == "0"
                  ? selectedTimingSlot[i] ? WHITE : BLACK
                  : Colors.grey.withOpacity(0.5),
              fontSize: 15,
          ),
        ),

**Above code shows= "12:00 AM" I need to hide or remove "AM"**

Upvotes: 0

Views: 970

Answers (3)

Yayo Arellano
Yayo Arellano

Reputation: 3866

The way I do it is with this extension

extension StringExtension on String {
  String deleteLastChar({int toDelete = 1}) => substring(0, length - toDelete);
}

And you can use like

"12:00 AM".deleteLastChar(toDelete: 3) // Prints 12:00

Why toDelete: 3? Because you also want to remove the space between 12:00 and AM

Upvotes: 0

Andrew Young-Min Cho
Andrew Young-Min Cho

Reputation: 67

Ketan’s substring method is a terrible way of doing this, what about “9:00 PM”?

Edit: looks like his method worked perfectly!

Use regex and/or the following package:

https://pub.dev/packages/string_validator

Upvotes: 0

Ketan Ramteke
Ketan Ramteke

Reputation: 10655

Use substring method:

main() {
  print("12:00 AM".substring(0,5));
}

Or with replaceAll method:

main() {
  print("12:00 AM".replaceAll("AM","").replaceAll("PM",""));
}

with regular expression:

main() {
  var regex = new RegExp(r'[a-zA-Z]');
  print("02:00 AM".replaceAll(regex,""));
}

Upvotes: 5

Related Questions