superscriptLastCharacter function

String superscriptLastCharacter(
  1. String value
)

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:

  • 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;
}