mirror of
https://github.com/hanskokx/arcane_helper_utils.git
synced 2026-05-14 10:29:07 +02:00
@@ -0,0 +1,61 @@
|
||||
/// Provides a quick shortcut to common strings, such as punctuation marks that are otherwise
|
||||
/// cumbersome to find or type.
|
||||
abstract class CommonString {
|
||||
/// An em dash (`—`) is commonly used in typography to set off parenthetical
|
||||
/// phrases or provide emphasis in a sentence.
|
||||
static const String emDash = "—";
|
||||
}
|
||||
|
||||
extension Nullability on String? {
|
||||
/// Returns `true` if the `String?` is neither `null` nor only contains whitespace.
|
||||
bool get isNotNullOrEmpty => !isNullOrEmpty;
|
||||
|
||||
/// Returns `true` if the `String?` is either `null` or contains only whitespace.
|
||||
bool get isNullOrEmpty => this == null || (this ?? "").trim().isEmpty;
|
||||
}
|
||||
|
||||
/// An extension on `String` to split the string into parts of a specified length.
|
||||
extension Split on String {
|
||||
/// Splits the string into a list of substrings, each with a maximum length of [length].
|
||||
///
|
||||
/// This method divides the string into chunks of size [length]. If the string's
|
||||
/// length is not a multiple of [length], the last chunk may be smaller than [length].
|
||||
///
|
||||
/// - [length]: The number of characters in each part.
|
||||
///
|
||||
/// Returns a list of substrings where each has a maximum of [length] characters.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// String text = "DartLang";
|
||||
/// List<String> result = text.splitByLength(3); // ["Dar", "tLa", "ng"]
|
||||
/// ```
|
||||
List<String> splitByLength(int length) {
|
||||
final List<String> parts = [];
|
||||
String string = this;
|
||||
|
||||
while (string.isNotEmpty) {
|
||||
parts.add(string.substring(0, length));
|
||||
string = string.substring(length);
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
}
|
||||
|
||||
/// An extension on `String` to perform text manipulation tasks.
|
||||
extension TextManipulation on String {
|
||||
/// Capitalizes the first letter of the string.
|
||||
///
|
||||
/// This method returns a new string where the first character is converted
|
||||
/// to uppercase, while the rest of the string remains unchanged.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// String text = "hello";
|
||||
/// String capitalized = text.capitalize; // "Hello"
|
||||
/// ```
|
||||
String get capitalize {
|
||||
if (isEmpty) return "";
|
||||
return "${this[0].toUpperCase()}${substring(1)}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user