Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Extract word from string (https://www.delphipraxis.net/129149-extract-word-string.html)

Razor 13. Feb 2009 12:57


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?

nahpets 13. Feb 2009 13:05

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;

GPRSNerd 13. Feb 2009 13:15

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);

stoxx 13. Feb 2009 13:25

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 18:05 Uhr.

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