Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Ununderstandable (for me) behavior concatenating strings

Posted on 2011-04-29
10
Medium Priority
?
253 Views
Last Modified: 2012-05-11
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.  
0
Comment
Question by:Marco Gasi
  • 5
  • 4
10 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35491156

What values are stored in the StringList

Also what does this give

 ShowMessage(sl.CommaText);
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 35491214
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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35492170

LOL, you've never used CommaText

We might need to explore the actual data in the list if you don't mind
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Author Comment

by:Marco Gasi
ID: 35492259
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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35492364

Looks like one of your strings is terminated by #0

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35492379
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
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 35492396

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
 
LVL 31

Author Closing Comment

by:Marco Gasi
ID: 35492453
Many thanks, ewangoya: it works as a charm. Bye
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35492464

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
 
LVL 31

Author Comment

by:Marco Gasi
ID: 35492519
Yet done, thanks :-)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question