Link to home
Start Free TrialLog in
Avatar of Anton007
Anton007

asked on

Need help with editing strings in a stringList

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);
Avatar of shaneholmes
shaneholmes

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
Avatar of Anton007

ASKER

Hi Shane

Thanks for that.


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

Thanks
Anton
Cut & paste me an example of the trings (lets say 10 records), showing the difference and I will  work something up for you

Shane

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
OK, i'll see what i can do...

Shane
is there always a number 1 here

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

Shane
No, That  "1" represents a qty. So that can be any number

Anotn
K

Shane
ASKER CERTIFIED SOLUTION
Avatar of shaneholmes
shaneholmes

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

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

Worked perfectly

Thank you!!

Cheers

Anton

<SMILE>

Im glad it worked!

Shane