Reputation: 1087
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
Upvotes: 80
Views: 65561
Reputation: 38
Leaving these extension methods here to be useful for anyone. I do not know if they are in some dart standard library, but I could not find them.
extension Substrings on String {
/// Takes the first n characters.
String takeFirst(int n) {
final int end = n > length ? length : n;
return substring(0, end);
}
/// Takes the last n characters.
String takeLast(int n) {
final int start = n > length ? 0 : length - n;
return substring(start, length);
}
/// Drops the first n characters.
String dropFirst(int n) {
return n > length ? "" : substring(length - n, length);
}
/// Drops the last n characters.
String dropLast(int n) {
return n > length ? "" : substring(0, length - n);
}
}
In your case you would want to use
string.takeLast(5);
Upvotes: 1
Reputation: 11
extension Operation on String? {
String lastChars(int n) {
if (this == null) return '';
return this!.characters.takeLast(n).string;
}
}
Maybe you can try this:
When n < str.length
, eg: 'object'.lastChars(2)
, you get ct
;
when n >= str.length
, and you get the whole string, eg: 'object'.lastChars(10)
, you get object
.
Upvotes: 1
Reputation: 115
I wrote my own solution to get any no of last n digits from a string of unknown length, for example the 5th to the last digit from an n digit string,
String bin='408 408 408 408 408 1888';// this is the your string
// this function is to remove space from the string and then reverse the
string, then convert it to a list
List reversed=bin.replaceAll(" ","").split('').reversed.toList();
//and then get the 0 to 4th digit meaning if you want to get say 6th to last digit, just pass 0,6 here and so on. This second reverse function, return the string to its initial arrangement
var list = reversed.sublist(0,4).reversed.toList();
var concatenate = StringBuffer();
// this function is to convert the list back to string
list.forEach((item){
concatenate.write(item);
});
print(concatenate);// concatenate is the string you need
Upvotes: 0
Reputation: 267784
Create an extension:
extension E on String {
String lastChars(int n) => substring(length - n);
}
Usage:
var source = 'Hello World';
var output = source.lastChars(5); // 'World'
Upvotes: 35
Reputation: 1630
var newString = string.substring((string.length - 5).clamp(0, string.length));
note: I am using clamp in order to avoid Value Range Error. By that you are also immune to negative n-characters if that is somehow calculated.
In fact I wonder that dart does not have such clamp implemented within the substring method.
If you want to be null aware, just use:
var newString = string?.substring((string.length - 5).clamp(0, string.length));
Upvotes: 5
Reputation: 5999
While @Alexandre Ardhuin is correct, it is important to note that if the string has fewer than n
characters, an exception will be raised:
Uncaught Error: RangeError: Value not in range: -5
It would behoove you to check the length before running it that way
String newString(String oldString, int n) {
if (oldString.length >= n) {
return oldString.substring(oldString.length - n)
} else {
// return whatever you want
}
}
While you're at it, you might also consider ensuring that the given string is not null.
oldString ??= '';
If you like one-liners, another options would be:
String newString = oldString.padLeft(n).substring(max(oldString.length - n, 0)).trim()
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. I know they are working on improving null safety and this might change in the future.
Upvotes: 14