Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last n-characters in a string in Dart?

Tags:

flutter

dart

How do I get the last n-characters in a string?

I've tried using:

var string = 'Dart is fun'; var newString = string.substring(-5); 

But that does not seem to be correct

like image 359
deantawonezvi Avatar asked Apr 29 '19 14:04

deantawonezvi


People also ask

How do you get the last n characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.

How do you find the last character of a string in darts?

If you expect it to always return a string with length of n , you could pad it with whatever default value you want ( . padLeft(n, '0') ), or just leave off the trim() . At least, as of Dart SDK 2.8. 1 , that is the case.

How do you get the last element in a list in darts?

you can use last property for read/write, inherited-getter: last is a returns the last element from the given list. The best one is lst[lst. length-1] way because in the "last" property ,it loops through the list until the last element is got , so it will be slower way.


1 Answers

var newString = string.substring(string.length - 5); 
like image 144
Alexandre Ardhuin Avatar answered Oct 13 '22 20:10

Alexandre Ardhuin