This chapter describes the sysutils unit. The sysutils unit was largely written by Gertjan Schouten, and completed by Michael Van Canneyt. It aims to be compatible to the Delphi sysutils unit, but in contrast with the latter, it is designed to work on multiple platforms. It is implemented on all supported platforms.
This chapter starts out with a definition of all types and constants that are defined, followed by an overview of functions grouped by functionality, and lastly the complete explanation of each function.
tfilename = string; tsyscharset = set of char; tintegerset = set of 0..sizeof(integer)*8-1; longrec = packed record lo,hi : word; end; wordrec = packed record lo,hi : byte; end; TMethod = packed record Code, Data: Pointer; end;The use and meaning of these types should be clear, so no extra information will be provided here.
The following general-purpose constants are defined:
const SecsPerDay = 24 * 60 * 60; // Seconds and milliseconds per day MSecsPerDay = SecsPerDay * 1000; DateDelta = 693594; // Days between 1/1/0001 and 12/31/1899 Eoln = #10;The following types are used frequently in date and time functions. They are the same on all platforms.
type TSystemTime = record Year, Month, Day: word; Hour, Minute, Second, MilliSecond: word; end ; TDateTime = double; TTimeStamp = record Time: integer; { Number of milliseconds since midnight } Date: integer; { One plus number of days since 1/1/0001 } end ;The following type is used in the FindFirst,FindNext and FindClose functions. The win32 version differs from the other versions. If code is to be portable, that part shouldn't be used.
Type THandle = Longint; TSearchRec = Record Time,Size, Attr : Longint; Name : TFileName; ExcludeAttr : Longint; FindHandle : THandle; {$ifdef Win32} FindData : TWin32FindData; {$endif} end;The following constants are file-attributes that need to be matched in the findfirst call.
Const faReadOnly = $00000001; faHidden = $00000002; faSysFile = $00000004; faVolumeId = $00000008; faDirectory = $00000010; faArchive = $00000020; faAnyFile = $0000003f;The following constants can be used in the FileOpen call.
Const fmOpenRead = $0000; fmOpenWrite = $0001; fmOpenReadWrite = $0002;The following constants can be used in the FileSeek call.
Const fsFromBeginning = 0; fsFromCurrent = 1; fsFromEnd = 2;The following variables are used in the case translation routines.
type TCaseTranslationTable = array[0..255] of char; var UpperCaseTable: TCaseTranslationTable; LowerCaseTable: TCaseTranslationTable;The initialization code of the sysutils unit fills these tables with the appropriate values. For the win32 and go32v2 versions, this information is obtained from the operating system.
The following constants control the formatting of dates. For the Win32 version of the sysutils unit, these constants are set according to the internationalization settings of Windows by the initialization code of the unit.
Const DateSeparator: char = '-'; ShortDateFormat: string = 'd/m/y'; LongDateFormat: string = 'dd" "mmmm" "yyyy'; ShortMonthNames: array[1..12] of string[128] = ('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'); LongMonthNames: array[1..12] of string[128] = ('January','February','March','April', 'May','June','July','August', 'September','October','November','December'); ShortDayNames: array[1..7] of string[128] = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); LongDayNames: array[1..7] of string[128] = ('Sunday','Monday','Tuesday','Wednesday', 'Thursday','Friday','Saturday');
The following constants control the formatting of times. For the Win32 version of the sysutils unit, these constants are set according to the internationalization settings of Windows by the initialization code of the unit.
Const ShortTimeFormat: string = 'hh:nn'; LongTimeFormat: string = 'hh:nn:ss'; TimeSeparator: char = ':'; TimeAMString: string[7] = 'AM'; TimePMString: string[7] = 'PM';
The following constants control the formatting of currencies and numbers. For the Win32 version of the sysutils unit, these constants are set according to the internationalization settings of Windows by the initialization code of the unit.
Const DecimalSeparator : Char = '.'; ThousandSeparator : Char = ','; CurrencyDecimals : Byte = 2; CurrencyString : String[7] = '$'; { Format to use when formatting currency : 0 = $1 1 = 1$ 2 = $ 1 3 = 1 $ 4 = Currency string replaces decimal indicator. e.g. 1$50 } CurrencyFormat : Byte = 1; { Same as above, only for negative currencies: 0 = ($1) 1 = -$1 2 = $-1 3 = $1- 4 = (1$) 5 = -1$ 6 = 1-$ 7 = 1$- 8 = -1 $ 9 = -$ 1 10 = $ 1- } NegCurrFormat : Byte = 5;The following types are used in various string functions.
type PString = ^String; TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency);The following constants are used in the file name handling routines. Do not use a slash of backslash character directly as a path separator; instead use the OsDirSeparator character.
Const DirSeparators : set of char = ['/','\']; {$ifdef Linux} OSDirSeparator = '/'; {$else} OsDirSeparator = '\'; {$endif}
Various date and time formatting routines accept a format string. to format the date and or time. The following characters can be used to control the date and time formatting:
Program Example1; { This program demonstrates the Date function } uses sysutils; Var YY,MM,DD : Word; Begin Writeln ('Date : ',Date); DeCodeDate (Date,YY,MM,DD); Writeln (format ('Date is (DD/MM/YY): %d/%d/%d ',[dd,mm,yy])); End.
Program Example2; { This program demonstrates the DateTimeToFileDate function } Uses sysutils; Begin Writeln ('FileTime of now would be: ',DateTimeToFileDate (Now)); End.
Program Example3; { This program demonstrates the DateTimeToStr function } Uses sysutils; Begin Writeln ('Today is : ',DateTimeToStr(Now)); Writeln ('Today is : ',FormatDateTime('c',Now)); End.
for a list of characters that can be used in the FormatStr formatting string, see section formatchars.
Program Example4; { This program demonstrates the DateTimeToString function } Uses sysutils; Procedure today (Fmt : string); Var S : AnsiString; begin DateTimeToString (S,Fmt,Date); Writeln (S); end; Procedure Now (Fmt : string); Var S : AnsiString; begin DateTimeToString (S,Fmt,Time); Writeln (S); end; Begin Today ('"Today is "dddd dd mmmm y'); Today ('"Today is "d mmm yy'); Today ('"Today is "d/mmm/yy'); Now ('''The time is ''am/pmh:n:s'); Now ('''The time is ''hh:nn:ssam/pm'); Now ('''The time is ''tt'); End.
Program Example5; { This program demonstrates the DateTimeToSystemTime function } Uses sysutils; Var ST : TSystemTime; Begin DateTimeToSystemTime(Now,ST); With St do begin Writeln ('Today is ',year,'/',month,'/',Day); Writeln ('The time is ',Hour,':',minute,':',Second,'.',MilliSecond); end; End.
Program Example6; { This program demonstrates the DateTimeToTimeStamp function } Uses sysutils; Var TS : TTimeStamp; Begin TS:=DateTimeToTimeStamp (Now); With TS do begin Writeln ('Now is ',time,' millisecond past midnight'); Writeln ('Today is ' ,Date,' days past 1/1/0001'); end; End.
Program Example7; { This program demonstrates the DateToStr function } Uses sysutils; Begin Writeln(Format ('Today is: %s',[DateToStr(Date)])); End.
Program Example8; { This program demonstrates the DayOfWeek function } Uses sysutils; Begin Writeln ('Today''s day is ',LongDayNames[DayOfWeek(Date)]); End.
Program Example9; { This program demonstrates the DecodeDate function } Uses sysutils; Var YY,MM,DD : Word; Begin DecodeDate(Date,YY,MM,DD); Writeln (Format ('Today is %d/%d/%d',[dd,mm,yy])); End.
Program Example10; { This program demonstrates the DecodeTime function } Uses sysutils; Var HH,MM,SS,MS: Word; Begin DecodeTime(Time,HH,MM,SS,MS); Writeln (format('The time is %d:%d:%d.%d',[hh,mm,ss,ms])); End.
The parameters must lie withing valid ranges (boundaries included):
Program Example11; { This program demonstrates the EncodeDate function } Uses sysutils; Var YY,MM,DD : Word; Begin DecodeDate (Date,YY,MM,DD); WriteLn ('Today is : ',FormatDateTime ('dd mmmm yyyy',EnCodeDate(YY,Mm,Dd))); End.
The parameters must have a valid range (boundaries included):
Program Example12; { This program demonstrates the EncodeTime function } Uses sysutils; Var Hh,MM,SS,MS : Word; Begin DeCodeTime (Time,Hh,MM,SS,MS); Writeln ('Present Time is : ',FormatDateTime('hh:mm:ss',EnCodeTime (HH,MM,SS,MS))); End.
Program Example13; { This program demonstrates the FileDateToDateTime function } Uses sysutils; Var ThisAge : Longint; Begin Write ('ex13.pp created on :'); ThisAge:=FileAge('ex13.pp'); Writeln (DateTimeToStr(FileDateToDateTime(ThisAge))); End.
Program Example14; { This program demonstrates the FormatDateTime function } Uses sysutils; Var ThisMoment : TDateTime; Begin ThisMoment:=Now; Writeln ('Now : ',FormatDateTime('hh:mm',ThisMoment)); Writeln ('Now : ',FormatDateTime('DD MM YYYY',ThisMoment)); Writeln ('Now : ',FormatDateTime('c',ThisMoment)); End.
Program Example15; { This program demonstrates the IncMonth function } Uses sysutils; Var ThisDay : TDateTime; Begin ThisDay:=Date; Writeln ('ThisDay : ',DateToStr(ThisDay)); Writeln ('6 months ago :',DateToStr(IncMonth(ThisDay,-6))); Writeln ('6 months from now :' ,DateToStr(IncMonth(ThisDay,6))); Writeln ('12 months ago :',DateToStr(IncMonth(ThisDay,-12))); Writeln ('12 months from now :' ,DateToStr(IncMonth(ThisDay,12))); Writeln ('18 months ago :',DateToStr(IncMonth(ThisDay,-18))); Writeln ('18 months from now :' ,DateToStr(IncMonth(ThisDay,18))); End.
Program Example16; { This program demonstrates the IsLeapYear function } Uses sysutils; Var YY,MM,dd : Word; Procedure TestYear (Y : Word); begin Writeln (Y,' is leap year : ',IsLeapYear(Y)); end; Begin DeCodeDate(Date,YY,mm,dd); TestYear(yy); TestYear(2000); TestYear(1900); TestYear(1600); TestYear(1992); TestYear(1995); End.
Use TTimeStamp variables if you need to keep very precise track of time.
Program Example17; { This program demonstrates the MSecsToTimeStamp function } Uses sysutils; Var MS : Comp; TS : TTimeStamp; DT : TDateTime; Begin TS:=DateTimeToTimeStamp(Now); Writeln ('Now in days since 1/1/0001 : ',TS.Date); Writeln ('Now in millisecs since midnight : ',TS.Time); MS:=TimeStampToMSecs(TS); Writeln ('Now in millisecs since 1/1/0001 : ',MS); MS:=MS-1000*3600*2; TS:=MSecsToTimeStamp(MS); DT:=TimeStampToDateTime(TS); Writeln ('Now minus 1 day : ',DateTimeToStr(DT)); End.
Program Example18; { This program demonstrates the Now function } Uses sysutils; Begin Writeln ('Now : ',DateTimeToStr(Now)); End.
The order of the digits (y/m/d, m/d/y, d/m/y) is determined from the
ShortDateFormat variable.
Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date)); TestStr('05/05/1999'); TestStr('5/5'); TestStr('5'); End.
The order of the digits (y/m/d, m/d/y, d/m/y) is determined from the
ShortDateFormat variable.
Program Example20; { This program demonstrates the StrToDateTime function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateTimeToStr(StrToDateTime(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Now)); TestStr('05-05-1999 15:50'); TestStr('5-5 13:30'); TestStr('5 1:30PM'); End.
Program Example21; { This program demonstrates the StrToTime function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',TimeToStr(StrToTime(S))); end; Begin teststr (TimeToStr(Time)); teststr ('12:00'); teststr ('15:30'); teststr ('3:30PM'); End.
Program Example22; { This program demonstrates the SystemTimeToDateTime function } Uses sysutils; Var ST : TSystemTime; Begin DateTimeToSystemTime(Now,ST); With St do begin Writeln ('Today is ',year,'/',month,'/',Day); Writeln ('The time is ',Hour,':',minute,':',Second,'.',MilliSecond); end; Writeln ('Converted : ',DateTimeToStr(SystemTimeToDateTime(ST))); End.
Program Example23; { This program demonstrates the Time function } Uses sysutils; Begin Writeln ('The time is : ',TimeToStr(Time)); End.
Program Example24; { This program demonstrates the TimeStampToDateTime function } Uses sysutils; Var TS : TTimeStamp; DT : TDateTime; Begin TS:=DateTimeToTimeStamp (Now); With TS do begin Writeln ('Now is ',time,' millisecond past midnight'); Writeln ('Today is ' ,Date,' days past 1/1/0001'); end; DT:=TimeStampToDateTime(TS); Writeln ('Together this is : ',DateTimeToStr(DT)); End.
Use TTimeStamp variables if you need to keep very precise track of time.
For an example, see MSecsToTimeStamp.
Program Example25; { This program demonstrates the TimeToStr function } Uses sysutils; Begin Writeln ('The current time is : ',TimeToStr(Time)); End.
These filenames are set in drivestr[0..26], and the first 4 have been preset to :
The AddDisk call adds Path to the names of drive files, and returns the number of the disk that corresponds to this drive. If you add more than 21 drives, the count is wrapped to 4.
The function returns True if the directory was successfully created, False otherwise.
Program Example26; { This program demonstrates the CreateDir and RemoveDir functions } { Run this program twice in the same directory } Uses sysutils; Begin If Not FileExists('NewDir') then If Not CreateDir ('NewDir') Then Writeln ('Failed to create directory !') else Writeln ('Created "NewDir" directory') Else If Not RemoveDir ('NewDir') Then Writeln ('Failed to remove directory !') else Writeln ('Removed "NewDir" directory'); End.
Remark Under LINUX, and Unix in general, the concept of disk is different than the DOS one, since the filesystem is seen as one big directory tree. For this reason, the DiskFree and DiskSize functions must be mimicked using filenames that reside on the partitions. For more information, see AddDisk
Program Example27; { This program demonstrates the DiskFree function } Uses sysutils; Begin Write ('Size of current disk : ',DiskSize(0)); Writeln (' (= ',DiskSize(0) div 1024,'k)'); Write ('Free space of current disk : ',Diskfree(0)); Writeln (' (= ',Diskfree(0) div 1024,'k)'); End.
Remark Under LINUX, and Unix in general, the concept of disk is different than the DOS one, since the filesystem is seen as one big directory tree. For this reason, the DiskFree and DiskSize functions must be mimicked using filenames that reside on the partitions. For more information, see AddDisk
For an example, see DiskFree.
Program Example28; { This program demonstrates the GetCurrentDir function } Uses sysutils; Begin Writeln ('Current Directory is : ',GetCurrentDir); End.
For an example, see CreateDir.
Program Example29; { This program demonstrates the SetCurrentDir function } Uses sysutils; Begin If SetCurrentDir ('..') Then Writeln ('Now in directory ',GetCurrentDir) else Writeln ('Change directory to .. failed.'); End.
If FileName doesn't have an extension, Extension is just appended.
Program Example31; { This program demonstrates the DeleteFile function } Uses sysutils; Var Line : String; F,I : Longint; Begin F:=FileCreate('test.txt'); Line:='Some string line.'#10; For I:=1 to 10 do FileWrite (F,Line[1],Length(Line)); FileClose(F); DeleteFile('test.txt'); End.
Program Example32; { This program demonstrates the DoDirSeparators function } {$H+} Uses sysutils; Procedure Testit (F : String); begin Writeln ('Before : ',F); DoDirSeparators (F); Writeln ('After : ',F); end; Begin Testit (GetCurrentDir); Testit ('c:\pp\bin\win32'); Testit ('/usr/lib/fpc'); Testit ('\usr\lib\fpc'); End.
Program Example33; { This program demonstrates the ExpandFileName function } Uses sysutils; Procedure Testit (F : String); begin Writeln (F,' expands to : ',ExpandFileName(F)); end; Begin Testit('ex33.pp'); Testit(ParamStr(0)); Testit('/pp/bin/win32/ppc386'); Testit('\pp\bin\win32\ppc386'); Testit('.'); End.
Program Example34; { This program demonstrates the ExtractFileName function } {$H+} Uses sysutils; Procedure Testit(F : String); begin Writeln ('FileName : ',F); Writeln ('Has Name : ',ExtractFileName(F)); Writeln ('Has Path : ',ExtractFilePath(F)); Writeln ('Has Extension : ',ExtractFileExt(F)); Writeln ('Has Directory : ',ExtractFileDir(F)); Writeln ('Has Drive : ',ExtractFileDrive(F)); end; Begin Testit (Paramstr(0)); Testit ('/usr/local/bin/mysqld'); Testit ('c:\pp\bin\win32\ppc386.exe'); Testit ('/pp/bin/win32/ppc386.exe'); End.
For an example, see ExtractFileDir.
For an example, see ExtractFileDir.
The full filename can always be reconstucted by concatenating the result of ExtractFilePath and ExtractFileName.
For an example, see ExtractFileDir.
The full filename can always be reconstucted by concatenating the result of ExtractFilePath and ExtractFileName.
For an example, see ExtractFileDir.
Note: This function does not exist in the Delphi unit.
Program Example35; { This program demonstrates the ExtractRelativePath function } Uses sysutils; Procedure Testit (FromDir,ToDir : String); begin Write ('From "',FromDir,'" to "',ToDir,'" via "'); Writeln (ExtractRelativePath(FromDir,ToDir),'"'); end; Begin Testit ('/pp/src/compiler','/pp/bin/win32/ppc386'); Testit ('/pp/bin/win32/ppc386','/pp/src/compiler'); Testit ('e:/pp/bin/win32/ppc386','d:/pp/src/compiler'); Testit ('e:\pp\bin\win32\ppc386','d:\pp\src\compiler'); End.
Program Example36; { This program demonstrates the FileAge function } Uses sysutils; Var S : TDateTime; fa : Longint; Begin fa:=FileAge('ex36.pp'); If Fa<>-1 then begin S:=FileDateTodateTime(fa); Writeln ('I''m from ',DateTimeToStr(S)) end; End.
For an example, see FileCreate
If a file with name FileName already existed on the disk, it is overwritten.
Program Example37; { This program demonstrates the FileCreate function } Uses sysutils; Var I,J,F : Longint; Begin F:=FileCreate ('test.dat'); If F=-1 then Halt(1); For I:=0 to 100 do FileWrite(F,I,SizeOf(i)); FileClose(f); F:=FileOpen ('test.dat',fmOpenRead); For I:=0 to 100 do begin FileRead (F,J,SizeOF(J)); If J<>I then Writeln ('Mismatch at file position ',I) end; FileSeek(F,0,fsFromBeginning); Randomize; Repeat FileSeek(F,Random(100)*4,fsFromBeginning); FileRead (F,J,SizeOf(J)); Writeln ('Random read : ',j); Until J>80; FileClose(F); F:=FileOpen('test.dat',fmOpenWrite); I:=50*SizeOf(Longint); If FileTruncate(F,I) then Writeln('SuccessFully truncated file to ',I,' bytes.'); FileClose(F); End.
Program Example38; { This program demonstrates the FileExists function } Uses sysutils; Begin If FileExists(ParamStr(0)) Then Writeln ('All is well, I seem to exist.'); End.
Program Example40; { This program demonstrates the FileGetAttr function } Uses sysutils; Procedure Testit (Name : String); Var F : Longint; Begin F:=FileGetAttr(Name); If F<>-1 then begin Writeln ('Testing : ',Name); If (F and faReadOnly)<>0 then Writeln ('File is ReadOnly'); If (F and faHidden)<>0 then Writeln ('File is hidden'); If (F and faSysFile)<>0 then Writeln ('File is a system file'); If (F and faVolumeID)<>0 then Writeln ('File is a disk label'); If (F and faArchive)<>0 then Writeln ('File is artchive file'); If (F and faDirectory)<>0 then Writeln ('File is a directory'); end else Writeln ('Error reading attribites of ',Name); end; begin testit ('ex40.pp'); testit (ParamStr(0)); testit ('.'); testit ('/'); End.
Program Example39; { This program demonstrates the FileGetDate function } Uses sysutils; Var F,D : Longint; Begin F:=FileCreate('test.dat'); D:=FileGetDate(D); Writeln ('File crerated on ',DateTimeToStr(FileDateToDateTime(D))); FileClose(F); DeleteFile('test.dat'); End.
Remark that you cannot open a file if it doesn't exist yet, i.e. it will not be created for you. If you want tp create a new file, or overwrite an old one, use the FileCreate function.
For an example, see FileRead
For an example, see FileOpen
Program Example41; { Program to demonstrate the FileSearch function. } Uses Sysutils; Const {$ifdef linux} FN = 'find'; P = '.:/bin:/usr/bin'; {$else} FN = 'find.exe'; P = 'c:\dos;c:\windows;c:\windows\system;c:\windows\system32'; {$endif} begin Writeln ('find is in : ',FileSearch (FN,P)); end.
Remark: The abovementioned constants do not exist in Delphi.
Program Example42; { This program demonstrates the FileSetAttr function } Uses sysutils; Begin If FileSetAttr ('ex40.pp',faReadOnly or faHidden)=0 then Writeln ('Successfully made file hidden and read-only.') else Writeln ('Coulnd''t make file hidden and read-only.'); End.
For an example, see FileCreate
Attr can be set to an OR-ed combination of the pre-defined faXXX constants.
The function returns zero of successfull.
For an example, see FileCreate.
The function returns the number of bytes written, or -1 in case of an error.
For an example, see FileCreate.
For an example, see FindFirst.
The Rslt record can be fed to subsequent calls to FindNext, in order to find other files matching the specifications.
remark: A FindFirst call must always be followed by a FindClose call with the same Rslt record. Failure to do so will result in memory loss.
Program Example43; { This program demonstrates the FindFirst function } Uses sysutils; Var Info : TSearchRec; Count : Longint; Begin Count:=0; If FindFirst ('*.pp',faAnyFile,Info)=0 then begin Repeat Inc(Count); With Info do Writeln (Name:40,Size:15); Until FindNext(info)<>0; end; FindClose(Info); Writeln ('Finished search. Found ',Count,' matches'); End.
remark: The last FindNext call must always be followed by a FindClose call with the same Rslt record. Failure to do so will result in memory loss.
For an example, see FindFirst
Program Example45; { This program demonstrates the GetDirs function } {$H+} Uses sysutils; Var Dirs : Array[0..127] of pchar; I,Count : longint; Dir,NewDir : String; Begin Dir:=GetCurrentDir; Writeln ('Dir : ',Dir); NewDir:=''; count:=GetDirs(Dir,Dirs); For I:=0 to Count do begin NewDir:=NewDir+'/'+StrPas(Dirs[I]); Writeln (NewDir); end; End.
Remark: you cannot rename across disks or partitions.
Program Example44; { This program demonstrates the RenameFile function } Uses sysutils; Var F : Longint; S : String; Begin S:='Some short file.'; F:=FileCreate ('test.dap'); FileWrite(F,S[1],Length(S)); FileClose(F); If RenameFile ('test.dap','test.dat') then Writeln ('Successfully renamed files.'); End.
Program Example47; { This program demonstrates the SetDirSeparators function } Uses sysutils; Begin Writeln (SetDirSeparators('/pp\bin/win32\ppc386')); End.
Most PChar functions are the same as their counterparts in the STRINGS unit. The following functions are the same :
Additionally, StrAlloc allocates 4 extra bytes to store the size of the allocated memory. Therefore this function is NOT compatible with the StrAlloc function of the Strings unit.
For an example, see StrBufSize.
Program Example46; { This program demonstrates the StrBufSize function } {$H+} Uses sysutils; Const S = 'Some nice string'; Var P : Pchar; Begin P:=StrAlloc(Length(S)+1); StrPCopy(P,S); Write (P, ' has length ',length(S)); Writeln (' and buffer size ',StrBufSize(P)); StrDispose(P); End.
For an example, see StrBufSize.
For an example, see StrPCopy.
For an example, see StrPas.
Program Example48; { This program demonstrates the AdjustLineBreaks function } Uses sysutils; Const S = 'This is a string'#13'with embedded'#10'linefeed and'+ #13'CR characters'; Begin Writeln (AdjustLineBreaks(S)); End.
Program Example49; { This program demonstrates the AnsiCompareStr function } {$H+} Uses sysutils; Procedure TestIt (S1,S2 : String); Var R : Longint; begin R:=AnsiCompareStr(S1,S2); Write ('"',S1,'" is '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('"',S2,'"'); end; Begin Testit('One string','One smaller string'); Testit('One string','one string'); Testit('One string','One string'); Testit('One string','One tall string'); End.
Program Example49; { This program demonstrates the AnsiCompareText function } {$H+} Uses sysutils; Procedure TestIt (S1,S2 : String); Var R : Longint; begin R:=AnsiCompareText(S1,S2); Write ('"',S1,'" is '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('"',S2,'"'); end; Begin Testit('One string','One smaller string'); Testit('One string','one string'); Testit('One string','One string'); Testit('One string','One tall string'); End.
Program Example51; { This program demonstrates the AnsiQuotedStr function } Uses sysutils; Var S : AnsiString; Begin S:='He said "Hello" and walked on'; S:=AnsiQuotedStr(Pchar(S),'"'); Writeln (S); Writeln(AnsiExtractQuotedStr(Pchar(S),'"')); End.
Program Example52; { This program demonstrates the AnsiLastChar function } Uses sysutils; Var S : AnsiString; L : Longint; Begin S:='This is an ansistring.'; Writeln ('Last character of S is : ',AnsiLastChar(S)); L:=Longint(AnsiLastChar(S))-Longint(@S[1])+1; Writeln ('Length of S is : ',L); End.
Remark On linux, no language setting is taken in account yet.
Program Example53; { This program demonstrates the AnsiLowerCase function } Uses sysutils; Procedure Testit (S : String); begin Writeln (S,' -> ',AnsiLowerCase(S)) end; Begin Testit('AN UPPERCASE STRING'); Testit('Some mixed STring'); Testit('a lowercase string'); End.
For an example, see AnsiExtractQuotedStr
Program Example54; { This program demonstrates the AnsiStrComp function } Uses sysutils; Procedure TestIt (S1,S2 : Pchar); Var R : Longint; begin R:=AnsiStrComp(S1,S2); Write ('"',S1,'" is '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('"',S2,'"'); end; Begin Testit('One string','One smaller string'); Testit('One string','one string'); Testit('One string','One string'); Testit('One string','One tall string'); End.
Program Example55; { This program demonstrates the AnsiStrIComp function } Uses sysutils; Procedure TestIt (S1,S2 : Pchar); Var R : Longint; begin R:=AnsiStrIComp(S1,S2); Write ('"',S1,'" is '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('"',S2,'"'); end; Begin Testit('One string','One smaller string'); Testit('One string','one string'); Testit('One string','One string'); Testit('One string','One tall string'); End.
Program Example58; { This program demonstrates the AnsiStrLastChar function } Uses sysutils; Var P : Pchar; L : Longint; Begin P:='This is an PChar string.'; Writeln ('Last character of P is : ',AnsiStrLastChar(P)); L:=Longint(AnsiStrLastChar(P))-Longint(P)+1; Writeln ('Length of P (',P,') is : ',L); End.
Program Example56; { This program demonstrates the AnsiStrLComp function } Uses sysutils; Procedure TestIt (S1,S2 : Pchar; L : longint); Var R : Longint; begin R:=AnsiStrLComp(S1,S2,L); Write ('First ',L,' characters of "',S1,'" are '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('those of "',S2,'"'); end; Begin Testit('One string','One smaller string',255); Testit('One string','One String',4); Testit('One string','1 string',0); Testit('One string','One string.',9); End.
Program Example57; { This program demonstrates the AnsiStrLIComp function } Uses sysutils; Procedure TestIt (S1,S2 : Pchar; L : longint); Var R : Longint; begin R:=AnsiStrLIComp(S1,S2,L); Write ('First ',L,' characters of "',S1,'" are '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('those of "',S2,'"'); end; Begin Testit('One string','One smaller string',255); Testit('ONE STRING','one String',4); Testit('One string','1 STRING',0); Testit('One STRING','one string.',9); End.
Remark On linux, no language setting is taken in account yet.
Program Example59; { This program demonstrates the AnsiStrLower function } Uses sysutils; Procedure Testit (S : Pchar); begin Writeln (S,' -> ',AnsiStrLower(S)) end; Begin Testit('AN UPPERCASE STRING'); Testit('Some mixed STring'); Testit('a lowercase string'); End.
Remark On linux, no language setting is taken in account yet.
Program Example60; { This program demonstrates the AnsiStrUpper function } Uses sysutils; Procedure Testit (S : Pchar); begin Writeln (S,' -> ',AnsiStrUpper(S)) end; Begin Testit('AN UPPERCASE STRING'); Testit('Some mixed STring'); Testit('a lowercase string'); End.
Remark On linux, no language setting is taken in account yet.
Program Example60; { This program demonstrates the AnsiUpperCase function } Uses sysutils; Procedure Testit (S : String); begin Writeln (S,' -> ',AnsiUpperCase(S)) end; Begin Testit('AN UPPERCASE STRING'); Testit('Some mixed STring'); Testit('a lowercase string'); End.
This function is provided for Delphi compatibility only, since it is completely equivalent to Dest:=Dest+S.
Program Example62; { This program demonstrates the AppendStr function } Uses sysutils; Var S : AnsiString; Begin S:='This is an '; AppendStr(S,'AnsiString'); Writeln ('S = "',S,'"'); End.
This function is provided for Delphi compatibility only. AnsiStrings are managed on the heap and should be preferred to the mechanism of dynamically allocated strings.
Program Example63; { This program demonstrates the AssignStr function } {$H+} Uses sysutils; Var P : PString; Begin P:=NewStr('A first AnsiString'); Writeln ('Before: P = "',P^,'"'); AssignStr(P,'A Second ansistring'); Writeln ('After : P = "',P^,'"'); DisposeStr(P); End.
Program Example64; { This program demonstrates the BCDToInt function } Uses sysutils; Procedure Testit ( L : longint); begin Writeln (L,' -> ',BCDToInt(L)); end; Begin Testit(10); Testit(100); Testit(1000); End.
It returns the following values:
Program Example65; { This program demonstrates the CompareStr function } {$H+} Uses sysutils; Procedure TestIt (S1,S2 : String); Var R : Longint; begin R:=CompareStr(S1,S2); Write ('"',S1,'" is '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('"',S2,'"'); end; Begin Testit('One string','One smaller string'); Testit('One string','one string'); Testit('One string','One string'); Testit('One string','One tall string'); End.
Program Example66; { This program demonstrates the CompareText function } {$H+} Uses sysutils; Procedure TestIt (S1,S2 : String); Var R : Longint; begin R:=CompareText(S1,S2); Write ('"',S1,'" is '); If R<0 then write ('less than ') else If R=0 then Write ('equal to ') else Write ('larger than '); Writeln ('"',S2,'"'); end; Begin Testit('One string','One smaller string'); Testit('One string','one string'); Testit('One string','One string'); Testit('One string','One tall string'); End.
This function is provided for Delphi compatibility only. AnsiStrings are managed on the heap and should be preferred to the mechanism of dynamically allocated strings.
For an example, see DisposeStr.
Program Example67; { This program demonstrates the FloatToStr function } Uses sysutils; Procedure Testit (Value : Extended); begin Writeln (Value,' -> ',FloatToStr(Value)); Writeln (-Value,' -> ',FloatToStr(-Value)); end; Begin Testit (0.0); Testit (1.1); Testit (1.1e-3); Testit (1.1e-20); Testit (1.1e-200); Testit (1.1e+3); Testit (1.1e+20); Testit (1.1e+200); End.
The meaning of the Precision and Digits parameter depends on the Format parameter. The format is controlled mainly by the Format parameter. It can have one of the following values:
Program Example68; { This program demonstrates the FloatToStrF function } Uses sysutils; Const Fmt : Array [TFloatFormat] of string[10] = ('general','exponent','fixed','number','Currency'); Procedure Testit (Value : Extended); Var I,J : longint; FF : TFloatFormat; begin For I:=5 to 15 do For J:=1 to 4 do For FF:=ffgeneral to ffcurrency do begin Write (Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : '); Writeln (FloatToStrf(Value,FF,I,J)); Write (-Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : '); Writeln (FloatToStrf(-Value,FF,I,J)); end; end; Begin Testit (1.1); Testit (1.1E1); Testit (1.1E-1); Testit (1.1E5); Testit (1.1E-5); Testit (1.1E10); Testit (1.1E-10); Testit (1.1E15); Testit (1.1E-15); Testit (1.1E100); Testit (1.1E-100); End.
The result is the number of characters that was copied in Buffer.
Program Example68; { This program demonstrates the FloatToStrF function } Uses sysutils; Const Fmt : Array [TFloatFormat] of string[10] = ('general','exponent','fixed','number','Currency'); Procedure Testit (Value : Extended); Var I,J : longint; FF : TFloatFormat; S : ShortString; begin For I:=5 to 15 do For J:=1 to 4 do For FF:=ffgeneral to ffcurrency do begin Write (Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : '); SetLength(S,FloatToText (@S[1],Value,FF,I,J)); Writeln (S); Write (-Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : '); SetLength(S,FloatToText (@S[1],-Value,FF,I,J)); Writeln (S); end; end; Begin Testit (1.1); Testit (1.1E1); Testit (1.1E-1); Testit (1.1E5); Testit (1.1E-5); Testit (1.1E10); Testit (1.1E-10); Testit (1.1E15); Testit (1.1E-15); Testit (1.1E100); Testit (1.1E-100); End.
Program Example70; { This program demonstrates the FmtStr function } Uses sysutils; Var S : AnsiString; Begin S:=''; FmtStr (S,'For some nice examples of fomatting see %s.',['Format']); Writeln (S); End.
'%' [Index':'] ['-'] [Width] ['.' Precision] ArgTypeelements between single quotes must be typed as shown without the quotes, and elements between square brackets [ ] are optional. The meaning of the different elements is shown below:
The argument type is determined from ArgType. It can have one of the following values (case insensitive):
In short, the E specifier formats it's arguument as follows:
FloatToStrF(Argument,ffexponent,Precision,3)
In short, the F specifier formats it's arguument as follows:
FloatToStrF(Argument,ffFixed,ffixed,9999,Precision)
In short, the G specifier formats it's arguument as follows:
FloatToStrF(Argument,ffGeneral,Precision,3)
In short, the M specifier formats it's arguument as follows:
FloatToStrF(Argument,ffCurrency,9999,Precision)
Program example71; {$mode objfpc} { This program demonstrates the Format function } Uses sysutils; Var P : Pointer; fmt,S : string; Procedure TestInteger; begin Try Fmt:='[%d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%%]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); fmt:='[%.4d]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%10.4d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10.4d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10.4d]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*d]';S:=Format (fmt,[4,5,10]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestHexaDecimal; begin try Fmt:='[%x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%10.4x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10.4x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10.4x]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*x]';S:=Format (fmt,[4,5,10]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestPointer; begin P:=Pointer(1234567); try Fmt:='[0x%p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%10p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%10.4p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%0:p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%0:10p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%0:10.4p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%0:-10p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[0x%0:-10.4p]';S:=Format (fmt,[P]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*p]';S:=Format (fmt,[4,5,P]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestString; begin try Fmt:='[%s]';S:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s); fmt:='[%0:s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s); fmt:='[%0:18s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s); fmt:='[%0:-18s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s); fmt:='[%0:18.12s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s); fmt:='[%-*.*s]';s:=Format(fmt,[18,12,'This is a string']);Writeln(fmt:12,'=> ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestExponential; begin Try Fmt:='[%e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%10.4e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10.4e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10.4e]';S:=Format (fmt,[1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,1.234]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestNegativeExponential; begin Try Fmt:='[%e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%10.4e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10.4e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10.4e]';S:=Format (fmt,[-1.234]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,-1.234]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestSmallExponential; begin Try Fmt:='[%e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%10e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%10.4e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10.4e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10e]';S:=Format (Fmt,[0.0123]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10.4e]';S:=Format (fmt,[0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,0.01234]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; Procedure TestSmallNegExponential; begin Try Fmt:='[%e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%10.4e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:10.4e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%0:-10.4e]';S:=Format (fmt,[-0.01234]);writeln(Fmt:12,' => ',s); Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,-0.01234]);writeln(Fmt:12,' => ',s); except On E : Exception do begin Writeln ('Exception caught : ',E.Message); end; end; writeln ('Press enter'); readln; end; begin TestInteger; TestHexadecimal; TestPointer; TestExponential; TestNegativeExponential; TestSmallExponential; TestSmallNegExponential; end.
Program Example72; { This program demonstrates the FormatBuf function } Uses sysutils; Var S : ShortString; Const Fmt : ShortString = 'For some nice examples of fomatting see %s.'; Begin S:=''; SetLength(S,FormatBuf (S[1],255,Fmt[1],Length(Fmt),['Format'])); Writeln (S); End.
Program Example73; { This program demonstrates the IntToHex function } Uses sysutils; Var I : longint; Begin For I:=0 to 31 do begin Writeln (IntToHex(1 shl I,8)); Writeln (IntToHex(15 shl I,8)) end; End.
Program Example74; { This program demonstrates the IntToStr function } Uses sysutils; Var I : longint; Begin For I:=0 to 31 do begin Writeln (IntToStr(1 shl I)); Writeln (IntToStr(15 shl I)); end; End.
Program Example75; { This program demonstrates the IsValidIdent function } Uses sysutils; Procedure Testit (S : String); begin Write ('"',S,'" is '); If not IsVAlidIdent(S) then Write('NOT '); Writeln ('a valid identifier'); end; Begin Testit ('_MyObj'); Testit ('My__Obj1'); Testit ('My_1_Obj'); Testit ('1MyObject'); Testit ('My@Object'); Testit ('M123'); End.
Program example88; { This program demonstrates the LastDelimiter function } uses SysUtils; begin Writeln(LastDelimiter('\.:','c:\filename.ext')); end.
Program Example76; { This program demonstrates the LeftStr function } Uses sysutils; Begin Writeln (LeftStr('abcdefghijklmnopqrstuvwxyz',20)); Writeln (LeftStr('abcdefghijklmnopqrstuvwxyz',15)); Writeln (LeftStr('abcdefghijklmnopqrstuvwxyz',1)); Writeln (LeftStr('abcdefghijklmnopqrstuvwxyz',200)); End.
Program Example77; { This program demonstrates the LowerCase function } Uses sysutils; Begin Writeln (LowerCase('THIS WILL COME out all LoWeRcAsE !')); End.
This function is obsolete, and shouldn't be used any more. The AnsiString mechanism also allocates ansistrings on the heap, and should be preferred over this mechanism.
For an example, see AssignStr.
Program Example78; { This program demonstrates the QuotedStr function } Uses sysutils; Var S : AnsiString; Begin S:='He said ''Hello'' and walked on'; Writeln (S); Writeln (' becomes'); Writeln (QuotedStr(S)); End.
If Count is larger than the actual length of S only the real length will be used.
Program Example79; { This program demonstrates the RightStr function } Uses sysutils; Begin Writeln (RightStr('abcdefghijklmnopqrstuvwxyz',20)); Writeln (RightStr('abcdefghijklmnopqrstuvwxyz',15)); Writeln (RightStr('abcdefghijklmnopqrstuvwxyz',1)); Writeln (RightStr('abcdefghijklmnopqrstuvwxyz',200)); End.
Program Example80; { This program demonstrates the StrFmt function } Uses sysutils; Var S : AnsiString; Begin SetLEngth(S,80); Writeln (StrFmt (@S[1],'For some nice examples of fomatting see %s.',['Format'])); End.
Program Example80; { This program demonstrates the StrFmt function } Uses sysutils; Var S : AnsiString; Begin SetLEngth(S,80); Writeln (StrLFmt (@S[1],80,'For some nice examples of fomatting see %s.',['Format'])); End.
To be successfully converted, a string can contain a combination of numerical characters, possibly preceded by a minus sign (-). Spaces are not allowed.
Program Example82; {$mode objfpc} { This program demonstrates the StrToInt function } Uses sysutils; Begin Writeln (StrToInt('1234')); Writeln (StrToInt('-1234')); Writeln (StrToInt('0')); Try Writeln (StrToInt('12345678901234567890')); except On E : EConvertError do Writeln ('Invalid number encountered'); end; End.
To be successfully converted, a string can contain a combination of numerical characters, possibly preceded by a minus sign (-). Spaces are not allowed.
Program Example82; {$mode objfpc} { This program demonstrates the StrToInt function } Uses sysutils; Begin Writeln (StrToIntDef('1234',0)); Writeln (StrToIntDef('-1234',0)); Writeln (StrToIntDef('0',0)); Try Writeln (StrToIntDef('12345678901234567890',0)); except On E : EConvertError do Writeln ('Invalid number encountered'); end; End.
If the string contains only spaces, an empty string is returned.
Program Example84; { This program demonstrates the Trim function } Uses sysutils; {$H+} Procedure Testit (S : String); begin Writeln ('"',Trim(S),'"'); end; Begin Testit (' ha ha what gets lost ? '); Testit (#10#13'haha '); Testit (' '); End.
If the string contains only spaces, an empty string is returned.
Program Example85; { This program demonstrates the TrimLeft function } Uses sysutils; {$H+} Procedure Testit (S : String); begin Writeln ('"',TrimLeft(S),'"'); end; Begin Testit (' ha ha what gets lost ? '); Testit (#10#13'haha '); Testit (' '); End.
If the string contains only spaces, an empty string is returned.
Program Example86; { This program demonstrates the TrimRight function } Uses sysutils; {$H+} Procedure Testit (S : String); begin Writeln ('"',TrimRight(S),'"'); end; Begin Testit (' ha ha what gets lost ? '); Testit (#10#13'haha '); Testit (' '); End.
Program Example87; { This program demonstrates the UpperCase function } Uses sysutils; Begin Writeln (UpperCase('this will come OUT ALL uPpErCaSe !')); End.