Maxefficiently
Maxefficiently

Reputation: 21

How do I split a long string into "wrapped" strings?

How to perform this in Delphi?

I have a very long string. I need to wrap sting every 5 letters and add a dot in the end of wrapped string.

Example string:

ssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Result:

sssss. 
sssss. 
sssss. 
sssss.

Upvotes: 0

Views: 1303

Answers (4)

Wouter van Nifterick
Wouter van Nifterick

Reputation: 24096

This one only works in recent Delphi's:

  for c in s do
  begin
    Write(c);
    if i mod maxSize=maxSize-1 then
      WriteLn('.');
    inc(i);
  end;

Iterate through all characters

  for i := 1 to length(s) do
  begin
    Write(s[i]);
    if i mod maxSize=0 then
      WriteLn('.');
  end;

Copy chunks

  for i := 0 to length(s) div maxSize do
    WriteLn(Copy(s,1+i*maxSize,maxSize),'.');

Upvotes: 0

Rob Kennedy
Rob Kennedy

Reputation: 163317

uses SysUtils;

Result := WrapText(s, '.'^M^J, [], 5);

But be careful of this note from the documentation:

WrapText does not insert a break into an embedded quoted string.

Upvotes: 4

Warren  P
Warren P

Reputation: 69012

Your teacher probably wants you to learn enough pascal to write something like this:

 loop through the characters in the string
   get a character from the string and add it to another string
   check if five letters have gone by, and if so, 
      add a dot and a carriage-return-and-linefeed character.
 end loop

Upvotes: 7

Uli Gerhardt
Uli Gerhardt

Reputation: 14001

Something along the lines of the following:

while Length(s) > 0 do
begin
  Result := Result + '. ' + Copy(s, ...);
  Delete(s, ...);
end;

Upvotes: 0

Related Questions