![]() |
Delphi-Version: 2010
Split strings using string list
Delphi-Quellcode:
Here is real API function from real project called INBarcodeOCR. It is nice problem demonstration.
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; 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? |
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:
[/edit]
strParse := TStringList.Create;
try strRow := TStringList.Create; try //Code finally strRow.Free; end; finally strParse.Free; end; |
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? |
AW: Re: Split strings using string list
Zitat:
|
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; |
AW: Split strings using string list
Wenn Du Parse und Row noch mit nil initialisierst...
|
AW: Split strings using string list
Zitat:
Beide Variablen werden vor dem try zugewiesen. Somit haben sie im finally definierte Werte. Es könnte lediglich passieren, daß im
Delphi-Quellcode:
eine Exception auftritt und dann das Parse nicht mehr freigegeben wird.
Row := TStringList.Create;
|
AW: Split strings using string list
Eben deswegen. Gerade himitsu reitet auf diesem Umstand ja gerne herum ;)
|
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. |
AW: Split strings using string list
Jaja, immer diese Ausflüchte :mrgreen:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:52 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz