Solved

Need help with editing strings in a stringList

Posted on 2004-04-03
11
191 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

777 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