Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Split strings using string list (https://www.delphipraxis.net/162202-split-strings-using-string-list.html)

WojTec 12. Aug 2011 10:24

Delphi-Version: 2010

Split strings using string list
 
Delphi-Quellcode:
function GetBarcodeCode (intNR : integer; AIndex : integer = 7): ansistring;
var
 strReturn : ansistring;
 strParse : TStringList;
 strRow : TStringlist;
 strCode : ansistring;
begin
 strReturn := GetBarcodesResult;
 strCode := '';
 if strReturn <> '' then
     begin
      strParse := TStringList.Create;
      strRow := TStringList.Create;
      strParse.Delimiter := #13;
      strParse.Text := strReturn;
      strRow.Delimiter := #9;
      if intNr <= strParse.Count then
          begin
          strRow.DelimitedText := strParse.Strings [intNR - 1];
          strCode := strRow.strings [AIndex - 1];
          end;
      FreeandNil (strParse);
      FreeandNil (strRow);
     end;
 GetBarcodeCode := strCode;
end;
Here is real API function from real project called INBarcodeOCR. It is nice problem demonstration.

Siply, it splits string first by CR control character and then by tabs. Split by CR is ok, but by tabs is invalid in case if space is available in string. If space is in string then it splits it as if this space is tab :shock: strRow should contain 8 items, because always in string are 7 tabs, but if space is inside, then it working as for 8 tabs :o

This is class bug or code is buggy? Can fix it using build-in Delphi functions?

DeddyH 12. Aug 2011 10:32

AW: Split strings using string list
 
Set StrictDelimiter of strRow to true. This wasn' t available in earlier versions of Delphi, but since Delphi 2006 you can use this property, so blanks will be ignored.

[edit] By the way: you should use try-finally-blocks to avoid memory leaks in case of exceptions.
Delphi-Quellcode:
strParse := TStringList.Create;
try
  strRow := TStringList.Create;
  try
    //Code
  finally
    strRow.Free;
  end;
finally
  strParse.Free;
end;
[/edit]

WojTec 12. Aug 2011 11:14

Re: Split strings using string list
 
Now is working :-D Thank you :)

I know it should be in try block, this is original function from SDK unit, I did it well in own implementation.

BTW: Can check version without long INC file?

DeddyH 12. Aug 2011 11:17

AW: Re: Split strings using string list
 
Zitat:

Zitat von WojTec (Beitrag 1116389)
BTW: Can check version without long INC file?

I do not know any different way, sorry.

himitsu 12. Aug 2011 11:20

AW: Split strings using string list
 
Also in etwa so:
Delphi-Quellcode:
function GetBarcodeCode(intNR: Integer; AIndex: Integer = 7): AnsiString;
var
  Parse, Row: TStringList;
begin
  Result := '';
  Row := nil;
  Parse := TStringList.Create;
  try
    Row := TStringList.Create;
    Parse.Text := GetBarcodesResult;
    Row.Delimiter := ' ';
    Row.StrictDelimiter := True;
    if (intNr > 0) and (intNr <= Parse.Count) then begin
      Row.DelimitedText := Parse[intNR - 1];
      if (AIndex > 0) and (AIndex <= Row.Count) then
        Result := Row[AIndex - 1];
    end;
  finally
    Parse.Free;
    Row.Free;
  end;
end;

DeddyH 12. Aug 2011 11:21

AW: Split strings using string list
 
Wenn Du Parse und Row noch mit nil initialisierst...

Uwe Raabe 12. Aug 2011 11:34

AW: Split strings using string list
 
Zitat:

Zitat von DeddyH (Beitrag 1116392)
Wenn Du Parse und Row noch mit nil initialisierst...

Warum?

Beide Variablen werden vor dem try zugewiesen. Somit haben sie im finally definierte Werte.

Es könnte lediglich passieren, daß im
Delphi-Quellcode:
Row := TStringList.Create;
eine Exception auftritt und dann das Parse nicht mehr freigegeben wird.

DeddyH 12. Aug 2011 12:05

AW: Split strings using string list
 
Eben deswegen. Gerade himitsu reitet auf diesem Umstand ja gerne herum ;)

himitsu 12. Aug 2011 12:36

AW: Split strings using string list
 
Och, wenn wirklich mal weniger, als 80 Byte frei sein oder wenn eh schon der Stack/Heap/EXE zerschossen ist, dann isses doch sowieso schon zu spät und man kann eh nix mehr retten.
(sind die einzigen Gründe, welche mir einfallen, daß eine TStringList sich nicht initialisieren läßt)

Aber OK, hab's mal geändert.
Ich mach es mir halt auch gerne mal einfach, wenn es keine wirklich "wichtigen" Gründe gibt, es doch mal vollkommen korrekt zu machen.
So, wenn es dort im Parse.Free knallt, dann würde Row nicht freigegeben, aber falls es "dort" tatsächlich einmal knallen sollte, dann isses nun wirklich zu spät und man sollte das Programm besser beenden/abschießen.

DeddyH 12. Aug 2011 12:40

AW: Split strings using string list
 
Jaja, immer diese Ausflüchte :mrgreen:


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:45 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz