Solved

Need help with editing strings in a stringList

Posted on 2004-04-03
11
189 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now