Recent

Author Topic: format-reading strings  (Read 18466 times)

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #15 on: August 12, 2009, 02:48:34 pm »
 :D
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
Re: format-reading strings
« Reply #16 on: August 12, 2009, 09:05:56 pm »
if you use lclproc, you can use the following too:
Code: [Select]
begin
  source := 'wueeuoieu: abcxxx';
  part := GetPart([':'], [], source, false, false);
  // part is now ' abcxxx'
end;
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: format-reading strings
« Reply #17 on: August 13, 2009, 12:42:00 am »
The tiOPF project available on SourceForge has a tiUtils.pas unit. There are some very handy utility functions in there. For your problem, there is tiToken(..) method, which could be used as follows:

 s := 'name: graeme';
 r1 := tiToken(s, ':', 1); // r1 now contains 'name'
 r2 := tiToken(s, ':', 2); // r2 now contains 'graeme'

There is also a much more advanced tiTokenLibrary.pas unit which supports more options. All of these could be copied and used in other projects without requiring tiOPF - they are simply value added functions/classes.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

gerardus

  • Jr. Member
  • **
  • Posts: 93
Re: format-reading strings
« Reply #18 on: August 13, 2009, 10:57:38 am »
You now have a few options, let me add one more:
If the amount of information that is to be linked to each of the items of the list is going to be important, or may change, what about using objects?
You can define a class to hold all the info you need:

e.g.

TMYClass = Class(TObject)
  Factor: String;
  Name: String;
  Whatever: Integer;
  ...
end;

Adding  Items to the listbox is simple:

Code: [Select]
var
 aThing: TMyClass;
begin
 aThing := TMyClass.Create;
 aThing.Factor := 'XXX';
 aThing.Name := 'YYY';
 ListBox.Items.AddObject('Factor: '+aThing.Factor+' Name: '+aThing.Name, aThing);
end;
Retrieving the info from the list item:

Code: [Select]
var
 aThing: TMyClass;
begin
 aThing := TMyClass(ListBox.Items.Objects[i]);
 ShowMessage(aThing.Name);

Don't forget to free the objects once you're done using the listbox (e.g. OnFormDestroy) :

Code: [Select]
procedure ClearListBox;
var
  i: integer;
begin
  for i := 0 to ListBox.Items.Count-1 do
    TMYClass(ListBox.Items.Objects[i]).Free;
end;

This may be much more efficient and manageable than parsing the text of the listbox items.

Regards,

Gerard.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: format-reading strings
« Reply #19 on: August 13, 2009, 11:14:56 am »
Don't forget to free the objects once you're done using the listbox
Assuming the data for the objects comes frome 'somewhere outside', you could use a TObjectList (unit contnrs) instead. Populate the ListBox with the items from the object list.
And when closing the application, simply free the objectlist. A TObjectList autofrees all objects when destroyed.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: format-reading strings
« Reply #20 on: August 13, 2009, 11:24:07 am »
This thread reminds me of this tv-spot (german)
http://www.youtube.com/watch?v=zl6WgCKfDuU

The little girl thinks she has a surprise egg, and asks "what's inside?"
As an answer she gets the whole plethora of "nutrition facts" which sounds scary to her.

gerardus

  • Jr. Member
  • **
  • Posts: 93
Re: format-reading strings
« Reply #21 on: August 13, 2009, 11:57:42 am »
He he, sometimes it's better not to ask.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: format-reading strings
« Reply #22 on: August 13, 2009, 04:41:03 pm »
Maybe we need a warning sign for Lazarus: do not open Pandora's box  O:-)
« Last Edit: August 13, 2009, 04:45:11 pm by eny »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

cazzajay

  • Jr. Member
  • **
  • Posts: 94
Re: format-reading strings
« Reply #23 on: August 14, 2009, 11:44:29 am »
i think its a homage to the size of the community out there devoted to good quality pascal programming...  8)
Windows XP 32 bit / Lazarus 1.0.6 / FPC 2.6.0

 

TinyPortal © 2005-2018