Mega Code Archive

 
Categories / Delphi / Strings
 

Dynamically Get Word or Character from String

Title: Dynamically Get Word or Character from String Question: How do i extract a any character or word from a string dynamically? Answer: Dynamically Cut a String from a String - Delphi ================================================================================ Type CharSet = set of char; const CS_Space : CharSet = [' ']; function GetToken(var InTxt : String; SpaceChar : CharSet) : String; var i : Integer; begin { Find first SpaceCharacter } i:=1; While (i { Get text upto that spacechar } Result := Copy(InTxt,1,i-1); { Remove fetched part from InTxt } Delete(InTxt,1,i); { Delete SpaceChars in front of InTxt } i:=1; While (i Delete(InTxt,1,i-1); end; {--------------------} { Usage example: } var s : String; begin s:='Any string, edit, or listboxitem'; showmessage(GetToken(s, CS_Space)); end; John O'Brien 2000 ICQ: 16431231 otakusata@yahoo.com