Ununderstandable (for me) behavior concatenating strings

Hi all.

Let say I have a StringList with six strings stored in. I need to produce a single string concatenating items 1, 2, 3, 4 and 5. All code I tried fails and only items 1 and 2 are concatenated in the new string.

1) s := Format('%S%S%S%S%S', [sl[1],sl[2],sl[3],sl[4],sl[5]]); 

2) s := sl[1]+sl[2]+sl[3]+sl[4]+sl[5];

3) s := '';
    for i := 0 to Pred(sl.Count) do
      if i > 0 then
        s := s +sl[i];

Open in new window


None of these constructs produces whished result. The humour is that if I write

for i := 0 to Pred(sl.Count) do
  ShowMessage(sl[i]);

Open in new window


I can view correctly each string holded by StringList, but if I write

ShowMessage(sl.Text);

Open in new window


then only the first three items are shown!

Any idea?

Thanks in advance.  
LVL 31
Marco GasiFreelancerAsked:
Who is Participating?
 
Ephraim WangoyaCommented:

When adding the strings to the stringlist, use Trim function

sl.Add(Trim(yourstring))

or you can clean the list using a loop as follows
for I := 0 to sl.Count - 1 do
      sl[I] := Trim(sl[I]);

    showmessage(sl.Text);

Open in new window

0
 
Ephraim WangoyaCommented:

What values are stored in the StringList

Also what does this give

 ShowMessage(sl.CommaText);
0
 
Marco GasiFreelancerAuthor Commented:
Thanks for answewring, ewangoya

1) They are normal alphanumeric strings.
2) sl.CommaText? Are you using some robot to read EE questions? I wrote sl_dot_Text sl.Text...
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ephraim WangoyaCommented:

LOL, you've never used CommaText

We might need to explore the actual data in the list if you don't mind
0
 
Marco GasiFreelancerAuthor Commented:
The actual data are

ASUSTeK Computer INC. A8N-VM/PVG
00426-OEM-8992662-00006
20101015064208.000000+120
178BFBFF00020F32
AT/AT COMPATIBLE A M I  - 3000606

They are given by several functions of magwmi library and I use them to create a hardware id.

Thast string you see above are copied and pasted from a TMemo: do you want laugh? Memo1.Lines.AddString(sl) perfectly works! I would avoid to use a hidden TMemo to access to the StringList items and really I don't understand this behavior.

Thanks for your help.
0
 
Ephraim WangoyaCommented:

Looks like one of your strings is terminated by #0

0
 
jimyXCommented:
When you say that your data is:
ASUSTeK Computer INC. A8N-VM/PVG
00426-OEM-8992662-00006
20101015064208.000000+120
178BFBFF00020F32
AT/AT COMPATIBLE A M I  - 3000606

That looks like five items starting from zero which makes the concatenation as follows:
s := Format('%S%S%S%S%S', [sl[0],sl[1],sl[2],sl[3],sl[4]]);

Open in new window

or
s := sl[0]+sl[1]+sl[2]+sl[3]+sl[4];

Open in new window

or
  s := '';
  for i := 0 to Pred(sl.Count) do
    s := s +sl[i];

Open in new window

even "ShowMessage(sl.Text);" works fine for me and shows the five lines. You must be doing something wrong. Can you show part of your code please, pertaining the StringList creation and filling.
0
 
Marco GasiFreelancerAuthor Commented:
Many thanks, ewangoya: it works as a charm. Bye
0
 
Ephraim WangoyaCommented:

An using your sample code, do this
s := '';
    for i := 0 to Pred(sl.Count) do
      if i > 0 then
        s := s + Trim(sl[i]);

Open in new window

0
 
Marco GasiFreelancerAuthor Commented:
Yet done, thanks :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.