Solved

Need help with editing strings in a stringList

Posted on 2004-04-03
11
195 Views
Last Modified: 2010-04-05
I have to open a text file read in each line into a TStringList.
Then I have to make changes to each string in the TListdtring
and write the chnaged strings to a list box.

Sample of the sting:
PART,1572315-111B,,,1,,1572315-111B,6

What I have to changes:
After the 1st comma I must insert ss1
After the 2nd comma I must insert ss2
After the 3rd comma I must insert ss3
and the 2nd last string must be 1572315-111B.cdl

The final string must look like this:
PART,1572315-111B,ss1,ss2,1,ss3,1572315-111B.cdl,6

I got this far:-

var
MyString : TStringList;
i : integer;
begin

  Mystring := TStringList.Create;
  Mystring.LoadFromFile('C:\Test.txt);

  for i := 0 to Mystring.Count-1 do
        listbox2.Items.Assign(Mystring);
0
Comment
Question by:Anton007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750452
Well, if all the strings are the same length (same thing) , you could simply:

var
 MyStringList : TStringList;
 I, J: integer;
 tmpStr: String;
begin
 MyStringList := TStringList.Create;
 MyStringList.LoadFromFile('Test.txt');
 for I := 0 to MyStringList.Count-1 do
 begin
  tmpStr:= MyStringList[I];
  Insert('ss1', tmpStr, 19);
  Insert('ss2', tmpStr, 23);
  Insert('ss3', tmpStr, 29);
  Insert('.cdl', tmpStr, 45);
  MyStringList[I]:= tmpStr;
 end; //for I
 MyStringList.Free;
end;

Shane
0
 

Author Comment

by:Anton007
ID: 10750488
Hi Shane

Thanks for that.


The strings are not the same length, What must i do then?

Thanks
Anton
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750492
Cut & paste me an example of the trings (lets say 10 records), showing the difference and I will  work something up for you

Shane
0
Technology Partners: 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!

 

Author Comment

by:Anton007
ID: 10750505

PART,1572315-112251B, ,,1,,1572315-112251B,6
PART,157231533-111C, ,,1,,157231533-111C,7
PART,1572315-11B, ,,1,,1572315-11B,68
PART,157231533-111XC, ,,1,,157231533-111XC,7


Thanks for the help
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750510
OK, i'll see what i can do...

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750541
is there always a number 1 here

PART,1572315-112251B, ,,  -----> 1 < -----  ,,1572315-112251B,6

Shane
0
 

Author Comment

by:Anton007
ID: 10750565
No, That  "1" represents a qty. So that can be any number

Anotn
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750572
K

Shane
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 500 total points
ID: 10750591
OK, try this (its crude, but seems to work)

{**************} is where your variable are (SS1, SS2, SS3) in case you need to replace them

Shane

 var
 MyStringList : TStringList;
 I, J, CommaCount: integer;
 tmpStr: String;
 PartStr: String;
 S: String;
begin
 MyStringList := TStringList.Create;
 MyStringList.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Test.txt');
 // step through list
 for I := 0 to MyStringList.Count-1 do
 begin
  tmpStr:= MyStringList[I]; //get record
  CommaCOunt:= 0;
  S:= '';
  // step through record (char by char)
  for J:= 0 to Length(tmpStr) do
  begin
   if tmpStr[J] = ',' then
   begin
    Inc(CommaCount);
    if CommaCount = 2 then
    begin
     PartStr:= Copy(tmpStr,0, J-1);
     S:= S + PartStr + ',' + 'ss1' + ','; {**************}
    end;
    if CommaCount = 3 then
    begin
     S:= S + 'ss2' + ','; {**************}
    end;
    if CommaCount = 5 then
    begin
     S:= S +  '1' + ',' + 'ss3';  {**************}
    end;
   end; //if tmpStr[J]
  end; //for J
  S:= S + Copy(PartStr,5,length(PartStr)) + '.cdl' + ',' + Copy(tmpStr, length(tmpStr),1);
  MyStringList[I]:= S; //put record back
 end; //for I
 MyStringList.Free;
end;
0
 

Author Comment

by:Anton007
ID: 10750634

Shane, Your the MAN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Worked perfectly

Thank you!!

Cheers

Anton
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750649

<SMILE>

Im glad it worked!

Shane
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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