nc-photos/np_common/lib/string_extension.dart

49 lines
1.3 KiB
Dart
Raw Permalink Normal View History

2021-12-02 09:02:51 +01:00
import 'dart:math' as math;
2021-04-10 06:28:12 +02:00
extension StringExtension on String {
/// Returns the string without any leading characters included in [characters]
String trimLeftAny(String characters) {
int i = 0;
while (i < length && characters.contains(this[i])) {
i += 1;
}
2021-09-15 08:58:06 +02:00
return substring(i);
2021-04-10 06:28:12 +02:00
}
/// Returns the string without any trailing characters included in
/// [characters]
String trimRightAny(String characters) {
int i = 0;
while (i < length && characters.contains(this[length - 1 - i])) {
i += 1;
}
2021-09-15 08:58:06 +02:00
return substring(0, length - i);
2021-04-10 06:28:12 +02:00
}
/// Returns the string without any leading and trailing characters included in
/// [characters]
String trimAny(String characters) {
return trimLeftAny(characters).trimRightAny(characters);
}
2021-10-29 13:46:51 +02:00
bool equalsIgnoreCase(String other) => toLowerCase() == other.toLowerCase();
2021-12-02 09:02:51 +01:00
String slice(int start, [int? stop]) {
if (start < 0) {
start = math.max(length + start, 0);
}
if (stop != null && stop < 0) {
stop = math.max(length + stop, 0);
}
if (start >= length) {
return "";
} else if (stop == null) {
return substring(start);
} else if (start >= stop) {
return "";
} else {
return substring(start, math.min(stop, length));
}
}
2021-04-10 06:28:12 +02:00
}