• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

Parsing text stored on a variable

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
jrmn
Asked:
jrmn
  • 5
2 Solutions
 
mikelittlewoodCommented:
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
 
mikelittlewoodCommented:
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
 
mikelittlewoodCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ZhaawZSoftware DeveloperCommented:
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
 
mikelittlewoodCommented:
Thanks ZhaawZ :)
0
 
mikelittlewoodCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now