superscriptLastCharacter function
Replaces the last character of the given value
with its superscript
equivalent if available.
-
Parameters:
value
: The string whose last character will be replaced.
-
Returns: A new string with the last character replaced by its superscript equivalent.
-
Throws:
- ArgumentError: If
value
is empty. - StateError: If there is no superscript equivalent for the last character.
- ArgumentError: If
-
Example:
final result = replaceLastCharacterWithSuperscript('x2'); print(result); // Outputs: x²
Implementation
String superscriptLastCharacter(String value) {
if (value.isEmpty) throw ArgumentError('Value cannot be empty');
final character = getLastChar(value);
final superscript = kSuperscripts[character.toLowerCase()];
if (isSuperscript(character)) return value;
if (superscript == null) {
throw StateError('Superscript equivalent not found for $character');
}
return value.substring(0, value.length - 1) + superscript;
}