Solved

splitting a master string in smaller sub-strings

Posted on 1999-01-22
17
216 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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