Solved

Need help with editing strings in a stringList

Posted on 2004-04-03
11
193 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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