Solved

splitting a master string in smaller sub-strings

Posted on 1999-01-22
17
217 Views
Last Modified: 2010-04-06
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
Comment
Question by:mathes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
  • +1
17 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 1362841
hi,
download hyperstring from the following address .its the best for string management routines

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

Regards Barry
0
 
LVL 3

Accepted Solution

by:
KE earned 30 total points
ID: 1362842
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
 
LVL 17

Expert Comment

by:inthe
ID: 1362843
nice coding K.E  :-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mathes
ID: 1362844
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1362845
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1362846
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
 
LVL 3

Expert Comment

by:KE
ID: 1362847
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
 
LVL 17

Expert Comment

by:inthe
ID: 1362848
yes KE i think he made a typo there
oh im such a good coder ;-)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362849
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
 
LVL 17

Expert Comment

by:inthe
ID: 1362850
no not the stars haha                  ¤
typo:       ¤                ¤                    ¤
first line mathes comment shoud read "dear ke" not "dear inthe"
            ¤                ¤                    ¤
waht si a tpyo?
¤                     ¤                ¤                   ¤

yarrB

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362851
you yeah :))

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

¤                        ¤

enough stars :))

vonavI rotkiV
0
 

Author Comment

by:mathes
ID: 1362852
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1362853
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
 

Author Comment

by:mathes
ID: 1362854
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1362855
well, that was a typo... i've never tested that, so I will and let you know how it goes....
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362856
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
 

Author Comment

by:mathes
ID: 1362857
Hi again,

yes, now everything works very well. Thank you.

With kind regards

Mathes


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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