Recent

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

laznoob32

  • New member
  • *
  • Posts: 7
How to convert TStrings to String?
« on: February 01, 2019, 10:05:48 pm »
I have a memo that the user can write a paragraph in and I have a button which when clicked adds the paragraph in Memo1 to Edit1. The problem is, the both handle different data types: TStrings and AnsiString respectively. Is there a simple way to resolve this issue?

Thanks in advance.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12269
  • FPC developer.
Re: How to convert TStrings to String?
« Reply #1 on: February 01, 2019, 10:20:48 pm »
add .text.   

Tstrings is a list of strings, and its .text parameter concatenates hem together. (or parsers one string into multiple ones, the other way around)

laznoob32

  • New member
  • *
  • Posts: 7
Re: How to convert TStrings to String?
« Reply #2 on: February 01, 2019, 10:41:13 pm »
add .text.   

Tstrings is a list of strings, and its .text parameter concatenates hem together. (or parsers one string into multiple ones, the other way around)
Did the trick, but what about newline? How can I represent a newline in the memo as a space in the Edit1?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12269
  • FPC developer.
Re: How to convert TStrings to String?
« Reply #3 on: February 01, 2019, 10:57:25 pm »
add .text.   

Tstrings is a list of strings, and its .text parameter concatenates hem together. (or parsers one string into multiple ones, the other way around)
Did the trick, but what about newline? How can I represent a newline in the memo as a space in the Edit1?

Assign to a string variable, run stringreplace on it. Then assign to the edit1.

laznoob32

  • New member
  • *
  • Posts: 7
Re: How to convert TStrings to String?
« Reply #4 on: February 01, 2019, 11:03:35 pm »
Thank you.

rvk

  • Hero Member
  • *****
  • Posts: 6777
Re: How to convert TStrings to String?
« Reply #5 on: February 01, 2019, 11:10:54 pm »
Instead of StringReplace you can also use DelimitedText from the TStringList.

Code: Pascal  [Select][+][-]
  1. Memo1.Lines.Delimiter := ' ';
  2. Edit1.Text := Memo1.Lines.DelimitedText;

Edit:
Ah, nuts. That doesn't quite work correct.
You'll get quotes and when you want to elimate them you get double spaces.

Code: Pascal  [Select][+][-]
  1. Memo1.Lines.Delimiter := ' ';
  2. Memo1.Lines.QuoteChar := ' ';
  3. Edit1.Text := Memo1.Lines.DelimitedText;
« Last Edit: February 01, 2019, 11:16:08 pm by rvk »

Bart

  • Hero Member
  • *****
  • Posts: 5573
    • Bart en Mariska's Webstek
Re: How to convert TStrings to String?
« Reply #6 on: February 01, 2019, 11:27:53 pm »
To make thing a little more interesting you can use:  O:-)
Code: Pascal  [Select][+][-]
  1.   Edit1.Text := Utf8EscapeControlChars(Memo1.Text, emAsciiControlNames);

It's in LazUtf8 unit.

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1530
    • Lebeau Software
Re: How to convert TStrings to String?
« Reply #7 on: February 02, 2019, 12:06:33 am »
You'll get quotes and when you want to elimate them you get double spaces.

Set the StrictDelimiter property to avoid that:

Code: Pascal  [Select][+][-]
  1. Memo1.Lines.Delimiter := ' ';
  2. Memo1.Lines.StrictDelimiter := True;
  3. Edit1.Text := Memo1.Lines.DelimitedText;

https://www.freepascal.org/docs-html/rtl/classes/tstrings.delimitedtext.html

Quote
If StrictDelimiter is set to True, then no quoting is done (The QuoteChar property is disregarded completely): the returned text will contain the items in the stringlist, separated by the Delimiter character. When writing the DelimitedText property, the text will be split at all occurrences of the Delimiter character; however, when reading, the QuoteChar property will be taken into account.
« Last Edit: February 02, 2019, 12:08:38 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

ASerge

  • Hero Member
  • *****
  • Posts: 2423
Re: How to convert TStrings to String?
« Reply #8 on: February 02, 2019, 12:13:34 am »
Code: Pascal  [Select][+][-]
  1. Memo1.Lines.Delimiter := ' ';
  2. Memo1.Lines.StrictDelimiter := True;
  3. Edit1.Text := Memo1.Lines.DelimitedText;
Lines containing spaces will be enclosed in quotation marks.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1530
    • Lebeau Software
Re: How to convert TStrings to String?
« Reply #9 on: February 02, 2019, 08:47:00 am »
Lines containing spaces will be enclosed in quotation marks.

Not according to the documentation I had quoted:

Quote
If StrictDelimiter is set to True, then no quoting is done (The QuoteChar property is disregarded completely)
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

ASerge

  • Hero Member
  • *****
  • Posts: 2423
Re: How to convert TStrings to String?
« Reply #10 on: February 02, 2019, 10:34:27 am »
Lines containing spaces will be enclosed in quotation marks.
Not according to the documentation I had quoted:
Still will. Test it. Just the documentation is not correct. The StrictDelimiter behavior is more correctly described in the Delphi documentation, and the FPC implementation makes it close to this.

rvk

  • Hero Member
  • *****
  • Posts: 6777
Re: How to convert TStrings to String?
« Reply #11 on: February 02, 2019, 10:37:53 am »
Lines containing spaces will be enclosed in quotation marks.

Not according to the documentation I had quoted:

Quote
If StrictDelimiter is set to True, then no quoting is done (The QuoteChar property is disregarded completely)

You're quoting the wrong line.

Quote
When writing the DelimitedText property, the text will be split at all occurrences of the Delimiter character; however, when reading, the QuoteChar property will be taken into account.
So when readin DelimtedText, QuoteChar is always taken into account. Try it.

In Delphi you can set QuoteChar to #0 (I think, haven't tested) but that doesn't work in FreePascal.
http://docwiki.embarcadero.com/Libraries/Rio/en/System.Classes.TStrings.QuoteChar
« Last Edit: February 02, 2019, 10:41:57 am by rvk »

djongepier

  • New Member
  • *
  • Posts: 12
Re: How to convert TStrings to String?
« Reply #12 on: May 15, 2025, 03:18:14 pm »
add .text.   

Tstrings is a list of strings, and its .text parameter concatenates hem together. (or parsers one string into multiple ones, the other way around)

This helped me too! Thanks!

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: How to convert TStrings to String?
« Reply #13 on: May 15, 2025, 05:18:53 pm »
what are the differences between TStrings and TStringList ?
in my Delphi Times, I had created:

var
  list: TStrings;
  lbl: TLabel;
begin
  list := TStringList.Create;
  list.Add('foo bar');
  ...
  lbl.Caption := list.Text;  // for one item in TStringList.
end;

But the point, I would know: why TStrings, and not TStringList in variable definition section ?
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 17196
  • Ceterum censeo Trump esse delendam
Re: How to convert TStrings to String?
« Reply #14 on: May 15, 2025, 05:49:15 pm »
  lbl.Caption := list.Text;  // for one item in TStringList.
end;
What is that for false information? >:( >:( >:( >:( >:(
It is ALL lines in the Tstrings in one single string with lineEndings..
Please no false information.
« Last Edit: May 15, 2025, 05:52:05 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018