numericOnly method

String numericOnly({
  1. bool firstWordOnly = false,
})

Extract numeric value of string Example: OTP 12312 27/04/2020 => 1231227042020ß If firstWordOnly is true, then the example return is "12312" (first found numeric word)

Implementation

String numericOnly({bool firstWordOnly = false}) {
  String numericOnlyStr = '';

  for (int i = 0; i < length; i++) {
    if (this[i].isNumericOnly) {
      numericOnlyStr += this[i];
    }
    if (firstWordOnly && numericOnlyStr.isNotEmpty && this[i] == " ") {
      break;
    }
  }

  return numericOnlyStr;
}