Search code examples
delphidelphi-xe2

Deleting char's in a ansiString


I have this ansiString for example

DISC506002000001008100021041511207123051520515308154091550920TN177869-0151J1     36J207 70077       0                 0

Trying to extract TN177869-0151J1

but the code I am using keeps returning me the whole ansistring.

function TForm5.ParseDataPartNumber(Data: AnsiString):ansistring;
var
   ExtraData: Ansistring;
begin
    extraData := data;
    Delete(extraData,76,30);
    Delete(extraData,0,61);
    result:=extraData;
end;

What am I doing wrong? Is it due to it being an ansistring instead of string? that is throwing me off?


Solution

  • Strings are 1 based, so change your 2nd Delete to index 1 instead of 0.

    ie:

    function TForm5.ParseDataPartNumber(Data: AnsiString):ansistring;
    var
       ExtraData: Ansistring;
    begin
        extraData := data;
        Delete(extraData,77,43);
        Delete(extraData,1,61);
        result:=extraData;
    end;
    

    Your indexes were wrong too to extract that string. My answer shows changed values.