Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split string based on length 2

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
So, I'm having to collect various pieces of information that are separate in some areas and combined in one other. The combined information has to be split up if the length is longer than 70. So, if the combined string is 80 characters, I need to split into a 70 and a 10. If the combined string is 150 characters I need to split it into 2 70 character strings and a 10.

Any suggestions?

Thanks!
Les
 
Something along the lines of this will split the string up into a StringList.
Code:
  while Length(instring) > 70 do begin
    stringlist.Add( LeftStr( instring, 70 ) );
    Delete(instring, 1, 70 );
  end;
  stringlist.Add( instring );

I haven't tested it but it will give you something to work on.

Andrew
Hampshire, UK
 
that's a great idea, way better than what I was thinking of!

Thanks!

leslie
 
March through your input string with a loop, the MidStr function and an index. Avoid modifying your input string.
 
That worked great!

The only issue I had was a 'Too many parameters' error on the Delete line. I searched the help and found that it's in the System file. When I added that to my uses clause it errored that it was a redeclaration of System. I searched the project and couldn't find System already declared. But I changed the line to System.Delete and it worked great!

Thanks!
les
 
The System unit is automatically used. It sounds as though you've defined a function or procedure with the name "Delete" that is hiding the System function.
 
No, I don't have a function or procedure named 'Delete' anywhere in my project and I'm not using any "extra" components that might have it either, just standard Delphi components.



Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for database developers:
The Fundamentals of Relational Database Design
Understanding SQL Joins
 
FYI, if you remove the System. prefix, and then rest your mouse over the Delete part, the Delphi IDE will tell you in which unit it is linking to a Delete procedure, and also tell you what parameters it is expecting.
 
well, Griffyn, that solved the mystery! The portion of code with the Delete was inside a WITH query DO, so it was looking for the TDataSet.Delete procedure.

That's another nifty tip to remember!

Thanks and have a great day!

Leslie
 
hello i am using your code to sample a string every 2 characters. i have used the same structure as you have:


stringlist.Create;
instring:= edit1.Text;
while Length(instring) > 2 do begin
stringlist.Add((leftstr((instring),2));

Delete(instring, 1, 2 );
end;
stringlist.Add( instring );


the error i get is:[Error] MainUnit.pas(205): Undeclared identifier: 'leftstr'

can you help me, i know it will most likely be a blatent mistake but as i am new to programing beaucse i am only doing my alevels.
 
you need to add StrUtils to the uses clause.

In the future if you highlight the function and press F1 the help page that opens for that function will show the unit that it is in, in this case it's the StrUtils unit.

HTH

Leslie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top