withoutSuffix method
Returns a copy of this
with suffix
removed if it is present.
If this
does not end with suffix
, returns this
.
Example:
var string = 'abc';
string.withoutSuffix('bc'); // 'a';
string.withoutSuffix('z'); // 'abc';
Implementation
String withoutSuffix(Pattern suffix) {
// Can't use endsWith because that takes a String, not a Pattern.
final matches = suffix.allMatches(this);
return (matches.isEmpty || matches.last.end != this.length)
? this
: this.substring(0, matches.last.start);
}