- Added additional `String` manipulation utilities, including:
  - `capitalizeWords`: Capitalizes the first letter of each word in the string.
  - `spacePascalCase`: Adds spaces between words in a PascalCase string.

Signed-off-by: Hans Kokx <hans.kokx@hackberry.se>
This commit is contained in:
Hans Kokx
2024-11-06 12:31:16 +01:00
parent c17cd7f6a3
commit 2c13b6fe72
3 changed files with 94 additions and 10 deletions
+42
View File
@@ -91,4 +91,46 @@ extension TextManipulation on String {
if (isEmpty) return "";
return "${this[0].toUpperCase()}${substring(1)}";
}
/// Capitalizes the first letter of each word in the string.
///
/// This method returns a new string where the first character of each word
/// is converted to uppercase, while the rest of the string remains unchanged.
///
/// Example:
/// ```dart
/// String text = "hello world";
/// String capitalizedWords = text.capitalizeWords; // "Hello World"
/// ```
String get capitalizeWords {
final strings = split(" ");
return strings.map((s) => s.capitalize).join(" ");
}
/// Adds spaces between words in a PascalCase string.
///
/// This method returns a new string where the capitalized words in a
/// PascalCase string are separated by spaces. The rest of the string remains
/// unchanged, aside from whitespace at the beginning and the end of the
/// string being stripped.
///
/// Example:
/// ```dart
/// String text = "ArcaneHelperUtils";
/// String spaced = text.spacePascalCase; // "Arcane Helper Utils"
/// ```
String get spacePascalCase {
final List<String> strings = split(
"",
);
String output = "";
for (final String char in strings) {
if (RegExp(r"[A-ZÄÖÅ]").hasMatch(char)) {
output += " $char";
} else {
output += char;
}
}
return output.trim();
}
}