Converting TStringlist to string with delimiter

22,507

Solution 1

You need to use the DelimitedText property of the TStringList class. From the online help

Use DelimitedText to get or set all the strings in the TStrings object in a single string, separated by the character specified by the Delimiter property.

Solution 2

use the DelimitedText property:

channelList.Delimiter := ',';
channelList.QuoteChar := ''; // or
channelList.QuoteChar := #0; // for higher delphi versions
aCurrentChannel := channelList.DelimitedText;

Solution 3

While you're into string lists i suggest you to cast a look at http://wiki.delphi-jedi.org/wiki/JCL_Help:IJclStringList

// var channelList: iJclStringList;
var s: string;

s := JclStringList.Add(['aaa','bbb','ccc '])
         .Split('ddd: eee', ':', False).Trim.Join(',');
Share:
22,507
Jeeva
Author by

Jeeva

There is always better way to do it so Refactor

Updated on May 22, 2020

Comments

  • Jeeva
    Jeeva almost 4 years

    I have a list of strings stored in TStringList, i want to convert it into string seperated by commas and i use the following code

    channelList: TStringList;
    aCurrentChannel :=  Stringreplace(channelList.Text,Char(13)+Char(10),',',[rfReplaceAll]);
    

    but the last character is coming as , like 1,2, is there anyway to avoid that?

  • Roman Marusyk
    Roman Marusyk over 8 years
    channelList.QuoteChar := ''; does not work anymore, channelList.QuoteChar := #0; - its working
  • Roman Marusyk
    Roman Marusyk over 8 years
    I'm using XE7 and I got an error: E2010 Incompatible types: 'Char' and 'string'. But in Delphi 6 I always used QuoteChar:= ''; It depends on unicode?
  • George Birbilis
    George Birbilis over 2 years
    Indeed, seems Char type doesn't allow '' but needs #0 in Delphi 11. I consider it counter-intuitive though, will file a report for it at quality.embarcadero.com