getTrailingInt method

int? getTrailingInt()

Gets the trailing integer from the string.

This method uses a regular expression to find the trailing digits in the string. If the string is empty or no trailing digits are found, it returns null.

Example:

String example = "FUZZY_NAME_60";
print(example.getTrailingInt()); // Output: 60

Implementation

int? getTrailingInt() {
  // Exit early if the string is empty
  if (isEmpty) {
    return null;
  }

  // Regular expression to match trailing digits
  final RegExp regex = RegExp(r'\d+$');

  final RegExpMatch? match = regex.firstMatch(this);

  // Return the parsed integer if a match is found, otherwise return null
  return match == null ? null : int.tryParse(match.group(0)!);
}