![]() |
Extract word from string
How to do it,for example
i want to extract some specific string from tlistbox. Items are: 100x200 200x300 1280x2000 if its listbox.items.strings[3}:='1280x2000' then i want to just extract only 1280 or 2000 from 1280x2000? Is it possible? |
Re: Extract word from string
Hallo,
Delphi-Quellcode:
Var
iPos : Integer; i1280 : Integer; i2000 : Integer; begin iPos := Pos('x',listbox.items.strings[3]); i1280 := StrToIntDef(Copy(listbox.items.strings[3],1,iPos - 1),0); i2000 := StrToIntDef(Copy(listbox.items.strings[3],iPos + 1,255),0); end; |
Re: Extract word from string
Something like this will do for the 1st and the 2nd number, assumed that they are always separated by an x:
Delphi-Quellcode:
copy(listbox.items.strings[3], 1, pos('x', listbox.items.strings[3])-1);
copy(listbox.items.strings[3], pos('x', listbox.items.strings[3])+1), MAX_INT); |
Re: Extract word from string
Or a general solution ...
Delphi-Quellcode:
function GetStringNr(const NR : integer; const Value : AnsiString; const delim : char = ';'): Ansistring;
var i, idx : integer; iOrd : byte; Res : AnsiString; Used : Integer; begin Result := ''; idx := 1; Used := 0; SetLength(Res, Length(Value)); for i := 1 to length(Value) do begin if Value[i] = delim then begin inc(IDX); continue; end; If IDX > NR then break; if IDX = NR then begin inc(Used); Res[used] := Value[i]; end; end; // von for SetLength(Res, Used); result := res; end; // von GetStringNRh //============================================================================== procedure TForm1.Button1Click(Sender : TObject); var Value1 : String; Value2 : String; begin Value1 := GetStringNR(1, listbox.items.strings[3], 'x'); Value2 := GetStringNR(2, listbox.items.strings[3], 'x'); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:10 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-2025 by Thomas Breitkreuz