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
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.
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.
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.
var newString = string.substring(string.length - 5);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With