Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

splitting a master string in smaller sub-strings

Hi experts,

I want to split this masterstring:


masterstring:= 'This is a sample string which should be examined by the experts in order to demonstrate which result I want
if I split a string.';


This string has a size of exactly 128 characters.


How can I split this big masterstring to 3 substrings with the following restriction:

a) None of the resulting sub-strings must exceed an individual lenghth of 50 characters.

b) The masterstring must not be split within a word. So if you find a non-blank at positions
   50 or 100 you must split at the first blank preceeding this word.

c) I know that my masterstring never exceeds a limit of 150 characters, so 3 resultstrings will be enough
   in any case to display the resulting substrings in editboxes.

For example: In my masterstring position 50 is the "y" within "by". In this case - as you must
not split the "by" - we need a split at the blank between"examined" and "by".
 
If the splitting procedure works correctly, you should get this result in the end:

edit1.text:='This is a sample string which should be examined';
edit2.text:='by the experts in order to demonstrate which';
edit3.text:='result I want if I split a string.';


How would you solve this splitting-problem ?


With kind regards

Mathes

0
mathes
Asked:
mathes
  • 7
  • 4
  • 4
  • +1
1 Solution
 
intheCommented:
hi,
download hyperstring from the following address .its the best for string management routines

http://www.mindspring.com/~efd/tools.htm

Regards Barry
0
 
KECommented:
Try this one :-)

const
  delimiters : set of char = [' '];

function SplitStrAt( var s: String; position: Integer ): String;
var
  i, mark : Integer;
begin
  for i := 1 to Length(s) do begin
    If (s[i] in delimiters) or (i=length(s)) then mark := i;
    If (i>=position) or (i=Length(s)) then begin
      Result := Copy(s, 1, mark);
      Delete(s, 1, Mark );
      break;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s : String;
begin
  s := 'This is a sample string which should be examined by the experts in order to demonstrate which result I want if I split a string.';
  While s<>'' do
    Memo1.Lines.Add( SplitStrAt( s, 50 ) );
end;

0
 
intheCommented:
nice coding K.E  :-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mathesAuthor Commented:
Dear inthe,

thank you for your posting. Your solution is almost perfect.

I have a question concerning the final step: Can you please show me,
how I can copy each line of the memo to a single editbox?
Please note thank I usually don't know the exact number of sub-strings before runtime.
I only know that I won't need more than three sub-strings, but sometimes even 1 or 2 substrings will be enough to save the splitted masterstring.

With kind regards

Mathes
 
0
 
viktornetCommented:
procedure TForm1.Button1Click(Sender: TObject);
   var
     s : String;
     i : integer;
   begin
     s := 'This is a sample string which should be examined by the experts in order to demonstrate which result I want if I split a string.';
     While s<>'' do begin
       for i := 0 to ComponentCount - 1 do
         if (Components[i] is TEdit) and TEdit(Components[i]).Text <> '' then
           TEdit(Components[i]).SplitStrAt(s, 50);
     end;
   end;

Hope this helps.. If you need more help let me know...

-Viktor
--Ivanov
0
 
viktornetCommented:
The edit boxes should be empty when you execute the code I gave you above so you might wanna do this....

procedure TForm1.Button1Click(Sender: TObject);
        var
          s : String;
          i : integer;
        begin
            for i := 0 to ComponentCount - 1 do
              if Components[i] is TEdit then
                TEdit(Components[i]).Clear;
          s := 'This is a sample string which should be examined by the experts in order
     to demonstrate which result I want if I split a string.';
          While s<>'' do begin
            for i := 0 to ComponentCount - 1 do
              if (Components[i] is TEdit) and TEdit(Components[i]).Text <> '' then
                TEdit(Components[i]).SplitStrAt(s, 50);
          end;
        end;
0
 
KECommented:
If you are always sure about the number of lines and which components should receive the result - try this, it's a bit more simplified:

s := 'blablabla....';
Edit1.Text := SplitStrAt( s, 50 );
Edit2.Text := SplitStrAt( s, 50 );
Edit3.Text := SplitStrAt( s, 50 );

Just keep on splitting the string - worse case would return an empty string ;-)

BTW. Thanks Inthe, nice to know that YOU appreciated it ;-) hehe

0
 
intheCommented:
yes KE i think he made a typo there
oh im such a good coder ;-)
0
 
viktornetCommented:
Yeah, KE is right... If you don't have lots of strings to split appart then you are okay, with Edit1.Text := SplitStrAt(s, 50);

inthe, who made a typo, and where is the typo? :))

¤¤¤

stars are falling everywhere...

Vik
0
 
intheCommented:
no not the stars haha                  ¤
typo:       ¤                ¤                    ¤
first line mathes comment shoud read "dear ke" not "dear inthe"
            ¤                ¤                    ¤
waht si a tpyo?
¤                     ¤                ¤                   ¤

yarrB

0
 
viktornetCommented:
you yeah :))

i might need to get a pair of glasses..
              ¤
some more stars...

¤                        ¤

enough stars :))

vonavI rotkiV
0
 
mathesAuthor Commented:
Hi experts,

thank you for your help. All your hints are very useful for me. I meanwhile ordered a license for Hyperstring. Inthe showed me how I can split a large string into sub-strings.
I like the way how victor counts the number of components in a form, those lines of code are very
instructive for me. There is just a little problem with victor's code.

In line

 if (Components[i] is TEdit) and TEdit(Components[i]).Text <> '' then


delphi says: "incompatible data types string and boolean".

How can you fix this problem ?

With kind regards

Mathes

0
 
viktornetCommented:
oh I forgot the parenthesis...sorry...here is how it should look like

if (Components[i] is TEdit) and (TEdit(Components[i]).Text <> '') then

Hope you learned something new from this :))

-Viktor
--Ivanov
0
 
mathesAuthor Commented:
Dear Victor,

yes, I indeed learned something new. I made the experience that I benefit much more from this online tutorial than I can profit from my printed books.

BTW: Even after your bugfix, the code still is incorrect.

Now in line :

if (Components[i] is TEdit) and (TEdit(Components[i]).Text <> '') then
         TEdit(Components[i]).SplitStrAt(s, 50);

"SplitStrAt" is an unknown identifier for Delphi.

I tried to correct it myself by writing:

if (Components[i] is TEdit) and (TEdit(Components[i]).Text <> '') then
TEdit(Components[i]).Text := SplitStrAt(s, 50);

Now the code is syntactically correct, but the 3 editboxes remain empty and no splitted sub-strings can be seen.

Is it possible to correct this mistake ?

With kind regards

Mathes

0
 
viktornetCommented:
well, that was a typo... i've never tested that, so I will and let you know how it goes....
0
 
viktornetCommented:
Ok, I found my mistake....

This line

if (Components[i] is TEdit) and (TEdit(Components[i]).Text <> '') then
     TEdit(Components[i]).Text := SplitStrAt(s, 50);

should have been..

if (Components[i] is TEdit) and (TEdit(Components[i]).Text = '') then
     TEdit(Components[i]).Text := SplitStrAt(s, 50);

I just exchanged the signs... sorry... Now it works perfectly...

You can simply use this code, hope this helps...

procedure TForm1.Button1Click(Sender: TObject);
var
  s : String;
  i : integer;
begin
  s := 'This is a sample string which should be examined by the experts in order to demonstrate which result I want if I split a string.';
  while s <> '' do
    for i := 0 to ComponentCount - 1 do
      if (Components[i] is TEdit)  then
        TEdit(Components[i]).Text := SplitStrAt(s, 50);
end;

-Viktor
--Ivanov
0
 
mathesAuthor Commented:
Hi again,

yes, now everything works very well. Thank you.

With kind regards

Mathes


0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now