-2

I have e-mail subject lines and I want to find ticket references in them it could be the TT ref is like 12345678. One subject line (string) can have multiple 8 digit numbers!

I have been using the below code but it is merely stripping out the first 8 digits then doing a check if that is 8 char long:

function StripNumbers(const aString: string): string;
var
  C: char;
begin
  Result := '';
  for C in aString do
  begin
    if CharInSet(C, ['0'..'9']) then
    begin
      Result := Result + C;
    end;
  end;
end;   

Example:

my string variable is

subject := "yada yada XF12345678 blabla XF87654321 duh XF11.223344"

function GetTTRefs(subject) should result "12345678;87654321;"

Thank you for answers.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
modzsi
  • 185
  • 2
  • 12
  • This code does not *strip out the first 8 digits*. It doesn't strip out anything, and it simply collects all of the digits from the string; it doesn't stop at 8, but will collect from zero to unlimited digits. You're going to need to be more clear what you're asking. – Ken White Mar 15 '17 at 00:45
  • @ken-white i know it just strips all non digit chars from a string and i do a check afterward if that result is 8 digit long. i have added an example. – modzsi Mar 15 '17 at 00:57

1 Answers1

-2
function GotTTRefs(Subject:string;Digits:Byte):string;
var
  i:integer;
  TT:string;
begin
   i:=1;
   while i <= Length(Subject)-Digits+1 do
   begin
     if Subject[i] in ['1'..'9'] then
        begin
          TT:=Copy(Subject,i,Digits);
          if (StrToQWordDef(TT, 0) <> 0) then
              Result:=Result+TT+';';
        end;
     inc(i);
   end;
end;  
modzsi
  • 185
  • 2
  • 12
  • That's pretty bad. You should iterate through the characters of the string, using array indexing `[i]` looking for values `>='0'` and `<='9'`. That way you don't need to perform any heap allocation until you find a value. – David Heffernan Mar 15 '17 at 08:41
  • thanks David, I have now re-wrote this based on your suggestion. – modzsi Mar 26 '17 at 19:54