Recent

Author Topic: How to convert TStrings to String?  (Read 15394 times)

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: How to convert TStrings to String?
« Reply #15 on: May 15, 2025, 05:55:26 pm »
I know that is ALL Text in TStringList.
But I mean:
- one TStringList.Add
- one Line + Linefeed

and that Line (or one Item) can be set to TLabel.Caption.

But I will not twist with you.
My Question stands under the Code.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

rvk

  • Hero Member
  • *****
  • Posts: 6778
Re: How to convert TStrings to String?
« Reply #16 on: May 15, 2025, 06:05:26 pm »
But the point, I would know: why TStrings, and not TStringList in variable definition section ?
TStrings is an abstract class. It can be used by other classes to inherit. For example TMemo.Lines. When you don't actually need the properties from the class which are in TStringList, it's best to use TStrings as your parameter so you can also pass one of those other classes.

I agree, when declaring list: TStrings; and creating it as TStringList it doesn't much matter, but if you are going to pass it to a function foo(AStrings: TStrings), which can both handle TStringList and the one from TMemo Lines, it does matter.

The same is the example of TAnimal.
You can do
Code: Pascal  [Select][+][-]
  1. var
  2.   MyPet: TAnimal;
  3. begin
  4.   MyPet := TCat.Create; // TCat is inherited from TAnimal
  5.   FeedMyAnimal(MyPet); // parameter is of type TAnimal

CM630

  • Hero Member
  • *****
  • Posts: 1409
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How to convert TStrings to String?
« Reply #17 on: May 15, 2025, 06:07:00 pm »
I am not sure that I understand you, but maybe this is what you need:


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     Label0: TLabel;
  18.     LabelAllStringSemicolon: TLabel;
  19.     LabelAllStringsComma: TLabel;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. var
  38.   myStringList : TStringList;
  39. begin
  40.   myStringList := TStringList.Create;
  41.   myStringList.add('0') ;
  42.   myStringList.add('1') ;
  43.   myStringList.add('2') ;
  44.   LabelAllStringsComma.caption  := myStringList.DelimitedText  ;
  45.   Label0.Caption := myStringList.Strings[0];
  46.   Label1.Caption := myStringList.Strings[1];
  47.   myStringList.StrictDelimiter :=True;
  48.   myStringList.Delimiter := ';';
  49.   LabelAllStringSemicolon.caption  := myStringList.DelimitedText  ;
  50. end;
  51. end.
  52.  

Note that .Delimter is a char, which is a pretty good reason not to use TStringList.
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1530
    • Lebeau Software
Re: How to convert TStrings to String?
« Reply #18 on: May 15, 2025, 06:35:35 pm »
what are the differences between TStrings and TStringList ?

TStrings is an abstract base class.  TStringList is a concrete descendant of TStrings.  There are other descendants available, such as used by the TMemo.Lines property, the TListBox.Items property, etc.

in my Delphi Times, I had created:
...
But the point, I would know: why TStrings, and not TStringList in variable definition section ?

In your particular example, it make no any difference whatsoever, as there is only 1 descendant involved, and everything is local to the procedure.  So use whichever type you want for the variable declaration.

But, in more complex situations, where multiple list objects/descendants are involved, or the variable is a function parameter, etc, then it makes more sense to declare the variable/parameter as TStrings to offer yourself flexibility in which object/descendant can be used, for example:

Code: Pascal  [Select][+][-]
  1. procedure AddToList(AStrings: TStrings);
  2. begin
  3.   AStrings.Add('foo bar');
  4. end;
  5.  
  6. procedure DoSomethingWithMemo;
  7. begin
  8.   ...
  9.   AddToList(Memo1.Lines);
  10.   ...
  11. end;
  12.  
  13. procedure DoSomethingWithListBox;
  14. begin
  15.   ...
  16.   AddToList(ListBox1.Items);
  17.   ...
  18. end;
  19.  
  20. procedure DoSomethingWithStringList;
  21. var
  22.   slList: TStringList;
  23. begin
  24.   ...
  25.   slList := TStringList.Create;
  26.   try
  27.     ...
  28.     AddToList(slList);
  29.     ...
  30.   finally
  31.     slList.Free;
  32.   end;
  33.   ...
  34. end;
  35.  
  36. procedure DoSomethingWithAlternateLists;
  37. var
  38.   listToAddTo: TStrings;
  39.   slList: TStringList;
  40. begin
  41.   ...
  42.   if SomeCondition then
  43.     listToAddTo := Memo1.Lines
  44.   else if SomeOtherCondition then
  45.     listToAddTo := ListBox1.Items
  46.   else begin
  47.     slList := TStringList.Create;
  48.     listToAddTo := slList;
  49.   end;
  50.   try
  51.     ...
  52.     AddToList(listToAddTo);
  53.     ...
  54.   finally
  55.     slList.Free;
  56.   end;
  57.   ...
  58. end;
  59.  
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018