[fpc-pascal]parsing text and working with strings

Michael Van Canneyt michael.vancanneyt at wisa.be
Fri Oct 19 09:26:10 CEST 2001


On Thu, 18 Oct 2001, the reverend wrote:

> what is the most efficient way to parse a space-delimited fixed width text
> file to an integer array?

Read line by line, copy width characters and convert to integer:

uses SysUtils;

Var
  F : Text;
  Line : String;
  Number : Integer;

begin
  // Assign file etc.
  While not EOF(F) do
    begin
    Read(F,Line);
    While Length(Line)>0 do
      begin
      // Change trim by TrimRight or TrimLeft if you know for sure the numbers
      // Are left or right aligned. That is more efficient.
      Number:=StrToInt(Trim(Copy(Line,1,Width)))
      Delete(Line,1,Width);
      // Store number somewhere.
      end;
    end;
end;


>
> also, i am rather new to freepascal and i have just discovered the plethora
> of string functions spread about in different libraries (strings, sysutils,
> and the system unit).  i would prefer to work with Strings, not PChar,
> because i am rather weak on working with pointers, but i have written some
> functions that basically make PChar functions into String functions for
> simplicity.  (and what is an AnsiString?)
>
> some functions i am looking for are:
>   1) strip duplicate delimiters, (can be used to remove extra spaces [#32]
> also)
>   2) parse delimited string to array
>   3) translate a table of characters in string to another table
>   4) ...

SysUtils contains routines that work on AnsiStrings. Ansistrings are like normal
pascal strings, but have unlimited length.

>
> is there a library that perhaps has a lot of this done already outside the
> standard freepascal libraries?  for simplicity's sake, i'd prefer to just
> work with a single type, hopefully Strings, but i can learn to use PChar if
> it is in my favor.

The SysUtils unit doesn't contain the specific calls you are looking for, but can
be used to construct the routines you want.

Michael.






More information about the fpc-pascal mailing list