Skip to content

Commit

Permalink
Add String.removePrefix()
Browse files Browse the repository at this point in the history
  • Loading branch information
mreichelt committed Jun 28, 2020
1 parent 314d2eb commit e2afa58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/src/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,14 @@ extension StringX on String {
}

bool get isNotNullOrEmpty => !this.isNullOrEmpty;

/// TODO: docs
String removePrefix(String prefix) {
if (startsWith(prefix)) {
return substring(prefix.length, length);
} else {
return this;
}
}

}
11 changes: 11 additions & 0 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,16 @@ void main() {
.md5,
'57edf4a22be3c955ac49da2e2107b67a');
});

test('.removePrefix()', () {
expect('abc def ghi'.removePrefix('abc'), ' def ghi');
expect(' abc def ghi'.removePrefix('abc'), ' abc def ghi');
expect('12345'.removePrefix('123456'), '12345');
expect('text'.removePrefix(''), 'text');
expect('🎉🌟'.removePrefix('🎉'), '🌟');

final expected = 'sameInstance';
expect(expected.removePrefix(''), same(expected));
});
});
}

0 comments on commit e2afa58

Please sign in to comment.