?
Solved

Parsing text stored on a variable

Posted on 2005-05-12
6
Medium Priority
?
145 Views
Last Modified: 2010-04-05
Hi Guys,

In my delphi 6 application, I have a variable (declared as string) named memostr which
depending on few given scenarios this can contain text of up to 1,500
characters (including spaces).
Given a button, itside it, I have declared 6 string variables namely, cmemo1, cmemo2,
cmemo3, cmemo4, cmemo5, cmemo6
What I wanted to do is that depending on the length of memostr (which will be 1,500 chars max),
it will be parse by 250 chars each and then to be stored on each of the 6 local variables.

Say if memostr = 750 chars
Then:

cmemo1 = chars 1 to 250 (from left to right) of memostr
cmemo2 = chars 251 to 500 (from left to right) of memostr
cmemo3 = chars 501 to 750 (from left to right) of memostr
cmemo4 = null or empty
cmemo5 = null or empty
cmemo6 = null or empty

Say if memostr = 400 chars
Then:

cmemo1 = chars 1 to 250 (from left to right) of memostr
cmemo2 = chars 251 to 400 (from left to right) of memostr
cmemo3 = null or empty
cmemo4 = null or empty
cmemo5 = null or empty
cmemo6 = null or empty


Say if memostr = 1009 chars
Then:

cmemo1 = chars 1 to 250 (from left to right) of memostr
cmemo2 = chars 251 to 500 (from left to right) of memostr
cmemo3 = chars 501 to 750 (from left to right) of memostr
cmemo4 = chars 751 to 1000 (from left to right) of memostr
cmemo5 = char 1001 to 1009 (from left to right) of memostr
cmemo6 = null or empty

Please show me code lines how to do this.

Thanks,
jrmn
0
Comment
Question by:jrmn
  • 5
6 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13987581
cmemo1 := Copy(memostr, 1, 250);
cmemo2 := Copy(memostr, 251, 500);
cmemo3 := Copy(memostr, 501, 750);
cmemo4 := Copy(memostr, 751, 1000);
cmemo5 := Copy(memostr, 1001, 1250);
cmemo6 := Copy(memostr, 1251, 1500);

0
 
LVL 15

Assisted Solution

by:mikelittlewood
mikelittlewood earned 80 total points
ID: 13987674
or you could use an array of strings to store each bit and a loop

var
  cmemoARR: array[0..5] of string;
  memostr: string;
  i: Integer;
begin
  for i := Low(cmemoARR) to High(cmemoARR) do
  begin
    // assign the string
    cmemoARR[i] := Copy(memostr, 1, 250);
    // strip out what you just saved
    memostr := Copy(memostr, 251, Length(memostr));
  end;

then just reference the array for each item
cmemoARR[0]
cmemoARR[1]
cmemoARR[2]
cmemoARR[3]
cmemoARR[4]
cmemoARR[5]
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13987899
If you have these variables globally, you might need to clear them each time before you repopulate them in case of old data.

cmemo1 := '';
cmemo2 := '';
cmemo3 := '';
cmemo4 := '';
cmemo5 := '';
cmemo6 := '';
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Accepted Solution

by:
ZhaawZ earned 120 total points
ID: 14000790
It's not necessary to delete copied text from main variable (as mikelittlewood said). If working with large amounts of data, it may slow down a program.

Clearing variables also is not needed. Quote from Delphi help:
"If Index [second parameter in copy() function] is larger than the length of S, Copy returns an empty string or array."


mikelittlewood already gave a good (the only?) example with using separate string variables, but his example with array has unnecessary deleting of copied text.


var
  txt : string;
  arr : array [0..5] of string;
  n : integer;

for n := 0 to high(arr) do arr[n] := copy(txt, n * 250 + 1, 250);


Btw, mikelittlewood, I believe Delete() is better than Copy() for deleting substring ;)
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 14005133
Thanks ZhaawZ :)
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 14005148
Clearing variables also is not needed. Quote from Delphi help:
"If Index [second parameter in copy() function] is larger than the length of S, Copy returns an empty string or array."

Ive never had an problem with this thought ZhaawZ.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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