Solved

splitting a master string in smaller sub-strings

Posted on 1999-01-22
17
214 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
  • 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now