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

前端 未结 4 775
误落风尘
误落风尘 2021-01-07 21:57

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);
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 22:47

    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'
    

提交回复
热议问题