Link to home
Start Free TrialLog in
Avatar of boardtc
boardtcFlag for Ireland

asked on

Escape Commas in TStrings.CommaText assignment

I want to assign a TStrings.CommaText to include commas, so I would like to be able to do something like :

  TStrings.CommaText := 'Create,Update'',''Delete,Empty';
so
  TStrings.Strings[0] = Create
  TStrings.Strings[1] = Update,Delete
  TStrings.Strings[2] = Empty

Is there any way to escape a comma in this assignment?

Thanks, Tom.
Avatar of Gani2001
Gani2001
Flag of Sudan image

Hi there
try to use this Simple Code

at first Declare this three Variable
 i: integer;
 SourceStr: string;
 StrList: TStringList;

and this the procedure body

StrList:= TStringList.Create;
SourceStr:= 'Create,Update'',''Delete,Empty';
SourceStr:=StringReplace(SourceStr,''',''','_',[rfReplaceAll,rfIgnoreCase]);
StrList.CommaText:= SourceStr;
for i:= 0 to StrList.Count-1 do
if Pos('_',StrList[i]) > 0 then
   StrList[i]:= StringReplace(StrList[i],'_',',',[rfReplaceAll,rfIgnoreCase]);

Abdelghani
Avatar of Eddie Shipman
Change the delimiter to something else, such as : and then use DelimitedText
instead of CommaText to Assgn the items. You can then also use CommaText
after assignment to get your items the same as using DelimitedText, or rather
the two calls will produce the same result.


MyStringList.Delimiter := ':';

// MUST BE NO SPACES because space is by default a delimiter
// in a stringlist when using CommaText or DelimitedText.
MyString.DelimitedText :=  'Create,Update:Delete:Empty';

Produces:
MyStringList[0] - 'Create,Update'
MyStringList[1] - 'Delete'
MyStringList[2] - 'Empty'
Avatar of snehanshu
snehanshu

To include commas in a comma-delimited text, you need to wrap the whole string into double quotes, not only the comma as you have done.
So, you need:
TStrings.CommaText := 'Create,"Update,Delete",Empty';

This will give you
  TStrings.Strings[0] = Create
  TStrings.Strings[1] = Update,Delete
  TStrings.Strings[2] = Empty

Hopt that helps,
...Snehanshu
tomcorcoran
  I guess you are aware of this but just for clarification: You cannot use TStrings class directly, so you would have to do it in some stringlist or combobox's items like
MyComboBox.Items.CommaText := 'Create,"Update,Delete",Empty';

 Also, note that " is double quote and NOT two quotes like ''
:-)
...Snehanshu
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boardtc

ASKER

perfect. thanks!

tom.
ehm,
guessing this was a grade by mistake,
i did not solved anything on this q

which one should be correct be graded, tom?
(i would then post q for this expert)

meikl ;-)
Avatar of boardtc

ASKER

opps, please reassing points to Snehanshu. thanks, tom.
Avatar of boardtc

ASKER

Snehanshu, sorry about this mistake. One used to be able to post a message on the community board but the format seems to have changed. Do you know how I can chnage the acepted answer from  kretzschmar to you?

Thanks, Tom.
doesn't matter,
i gave Snehanshu the same points from
my question-points, so i guess, there must be no action taken

meikl ;-)
Avatar of boardtc

ASKER

thanks for that, tom.
Thank you tomcorcoran. No problemo!
kretzschmar has passed the points to me.
https://www.experts-exchange.com/questions/20807210/for-Snehanshu.html
Cheers!
...Shu