Re: Delphi: Using only part of a stringPosted by webmaster Guido on January 07, 2004 at 14:52:27: In Reply to: Re: Using only part of a string posted by WAllison on January 03, 2004 at 21:14:12:
: : I have a problem when using my Delphi TListBox component it is.. : try something like: : Delete(ListBox1.Items[0].Caption, :
Delete(string, index, count) - deletes a substring within a string(note the first index
is 1 (if u use 0 - it deletes nothing) There are a few errors in this code: * It should be ListBox1.Items[0] instead of ListBox1.Items[0].Caption; ** You can only pass a string variable to the Pascal procedure Delete(). But ListBox1.Items[0] is not a variable, it's a property of a component, so Delete(ListBox1.Items[0], ...) is invalid. How it should be done: 1. Put the text of a listbox item in a string variable, say variable S: 2. Delete the first part of this string variable: var i: integer; S: string; begin i := ListBox1.ItemIndex; if i >= 0 then begin S := ListBox1.Items[i]; // S contains '20. Hello' Delete(S, 1, Pos('. ', S)); // S contains 'Hello' Label1.Caption := S; end; end; Example 2: remove the number part from every listbox item: var i: integer; S: string; begin for i := 0 to ListBox1.Items.Count - 1 do begin S := ListBox1.Items[i]; Delete(S, 1, Pos('. ', S)); ListBox1.Items[i] := S; end; end; [ DelphiLand: free Delphi source code, tips, tutorials ] |
|