Recent

Author Topic: Parsing Long string to specific sizes  (Read 2007 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Parsing Long string to specific sizes
« Reply #15 on: August 16, 2022, 09:10:01 pm »
WOW

Probably simple to you guys but I think the two parsing solutions are really brilliant. I don't know TStringlist or TStrings that well, just learning really, but I'm finally beginning to understand and appreciate the two solutions.

Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Parsing Long string to specific sizes
« Reply #16 on: September 02, 2022, 12:32:06 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. function PrepareLines(const AString: string; const ALineLength: Integer = 80): TStringArray;
  3. var
  4.   Strings: TStringArray;
  5.   tmp: string;
  6.   i, ii: Integer;
  7. begin
  8.   Strings := AString.Split(' ');
  9.   SetLength(Result, 0);
  10.   tmp := '';
  11.   for i := Low(Strings) to High(Strings) do
  12.     begin
  13.       if ((Length(Strings[i]) + Length(tmp) + 1) > ALineLength) then
  14.         begin
  15.           ii := Length(Result);
  16.           SetLength(Result, Succ(ii));
  17.           Result[ii] := tmp;
  18.           tmp := '';
  19.         end;
  20.       tmp := tmp + ' ' + Strings[i];
  21.     end;
  22.   if (tmp <> '') then
  23.     begin
  24.       ii := Length(Result);
  25.       SetLength(Result, Succ(ii));
  26.       Result[ii] := tmp;
  27.       tmp := '';
  28.     end;
  29. end;
  30.  
I guess what i do there is not what you was searching for, or?
This will generate an array of lines which not exceed a given limit (80 chars by default)
Afterwards you can easy iterate over like ... ->
Code: Pascal  [Select][+][-]
  1. var
  2.   sa: TStringArray;
  3.   i: Integer;
  4. begin
  5.   sa := PrepareLines('Header: xx bbb ccccc dddd eeee fffff gggg kk i jjjjj kkkk lll mmmm nnn ooo ppppp qqqqq rrrr ssssss tttttt uu vvv www xxx yy zzz.', 10);
  6.   for i := Low(sa) to High(sa) do
  7.     Memo1.Lines.Add(sa[i]);
  8.  
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018