Solved

Need help with editing strings in a stringList

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

Industry Leaders: 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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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