Delphi 7.0 Common functions Quick Check Manual

Source: Internet
Author: User
Tags floor function

      functions consist of one or more lines of code that can implement a particular function. Using functions can make your code more readable, easier to understand, faster to program, and less repetitive code. Procedure is similar to a function, the most important difference between a procedure and a function is that the procedure has no return value, and the function can have a return value.   in Delphi 7.0, a very large number of functions have been defined for us, roughly classified into 6 types: Data type conversion function, string, array manipulation function, file, disk operation function, memory, pointer manipulation function, mathematical operation function, Date function.   Call functions in Delphi, you can normally use functions directly, but because some functions are not included in the cells listed in uses (the default cell is Windows,messages,sysutils,variants,classes, Graphics, controls,forms,dialogs;), so we need to add the unit manually. For example, the MIDSTR function is not included in these cells, Midstr belongs to the unit in Strutils, so we will strutils add uses.   In this manual, all functions that are not included in the default list of units are marked with the units they belong to, and should be noted when used.   Data type conversion function in our programming, depending on the situation, we will use a variety of data types. When you want to operate on different types, you must convert different types to the same type. Therefore, it is very important to master the conversion of data type skillfully.    1.FLOATTOSTR Function Description: This function is used to convert "floating-point" to "character-type".  Reference example: Edit1.text: = Floattostr (1.981);   2.IntToStr function Description: This function is used to convert "integer" to "character" type. Example: s: = IntToStr (Ten);(Note: S is a string type variable.   3.IntToHex Function Description: This function is used to convert "decimal" to "decimal". The function has two parameters.  The first parameter is the decimal data to be converted, and the second parameter specifies how many bits are used to display the hexadecimal data.  Reference example: Edit1.text: = Inttohex (' 100 ', 2);  Execution results, Edit1.text equals 64. Note: Delphi does not provide a special "hex" function to convert to "decimal". This function can be implemented using the Strtoint function. The specific code is: I: = StRtoint (' s\ ' + ' 64 '); At this point I equals 100. Add a ' s\ ' to convert ' hex ' to ' decimal '.    4.STRTOINT Function Description: This function is used to convert "character type" to "integer".  Reference example: I: = Strtoint (' 100 '); Note: You cannot convert a type such as Strtoint (' ab ') or strtoint (' good ') because they do not exist as a digital type.    5.StrToFloat Function Description: This function is used to convert "character type" to "floating point type".  Reference example: N: = Strtofloat (Edit1.text); Note: The content in Edit1.text is 1.981 (all text displayed in the edit control is a string). N is a double type that holds the converted floating-point data.   Second, string, array manipulation functions for strings and arrays, which every programmer must master. Skilled use of these functions, in programming can be more handy.   1.Copy Function Description: This function is used to copy characters from a specified range from a string. The function has 3 parameters. The first parameter is the data source (that is, the copied string), the second parameter is copied from one point in the string, and the third parameter is the length (or number) of the string to copy.  The last function returns a new string (that is, the string content that we specify to copy).    Reference example: Var s:string; mystr:string;  Save new string begin S: = ' I love china! ';  The following will get the "love" string in I love Chinese.  MyStr: = Copy (S, 3, 4);  End Execution results, mystr equals "Love", "Love" string in "I Love china!" In the 3rd position, so the second argument is 3 and "Love" has 4 characters, so the third argument is 4.    2.Concat Function Description: Concatenate two or more strings into a string.  Reference example: Var S1, s2:string; Begin S1: = Concat (' A ', ' B ');    Connect two strings, S1 variable equals AB. S2: = Concat (' Borland ', ' Delphi ', ' 7.0 '); Connect three characters, S2 variable equals borland Delphi 7.0. end;  3.Delete Function Description: Removes the string specified in the string. The function has three parameters.  The first parameter is the string to be processed, the second parameter is where to start the deletion, and the third parameter is the number of characters deleted.  Reference example: Var s:string; Begin S: = ' I like Reading CPCW. ';     //The following code will remove the "C" character from the s variable.  Delete (S, 16, 1);  End At this point the s variable is I like Reading PCW. ("C" no longer exists).    4.High Function Description: Returns the maximum value of the array subscript.    Reference example: Var arrtext:array[0..9] of Char;  I:integer; Begin I: = High (arrtext); The value of I is 9 end;  5.Insert function Description: Insert a character (string). The function has three parameters.  The first argument is the character (string) to be inserted, the second argument is the inserted string (the source string), and the third argument is where to insert it.  Reference example: Var s:string;    Begin S: = ' Wat is your name? ';    The words in the above sentence look for an "H" character, the following uses the Insert function to add H. Insert (' h ', S, 2);  Insert "H" from 2nd place. end;  6.LeftStr (unit: strutils) function Description: Returns the number of new characters (strings) specified to the left of the string. The function has two parameters.  The first parameter is the complete string, and the second parameter is the specified number.  Reference example: Var S, a:string;   Begin S: = ' MSN Messenger ';     A: = Leftstr (S, 3); Starting at the far left, get the three characters on the left.  So the A variable equals MSN.  end;  7.Length Function Description: This function is used to count the length of the specified string (that is, the number). Reference example: Var nLen1, Nlen2:integer;   Used to save string length begin nLen1: = Length (' CPCW '); &nbsp   nlen2: = Length (' Computer newspaper ');  End Execution results, NLen1 equals 4,nlen2 equals 6. Since a Chinese character corresponds to a length of two characters, the length of the 3 kanji is 6.    8.Low Function Description: Returns the minimum value of the array subscript.    Reference example: Var arrtext:array[1..9] of Char;  I:integer; Begin i:= High (arrtext);  The value of I is 1 end;  9.LowerCase function Description: Converts characters (strings) of English characters to lowercase.  Reference example: Var S, a:string;    Begin S: = ' ABC '; A: = uppercase (S);  After the uppercase function is converted, A is equal to ABC. end;  10.MidStr (unit: strutils) function Description: Returns the string within the specified range. The function has three parameters. The first argument is the source string, the second argument is the starting point, and the third argument is the end point.  The second and third parameters specify the range of strings to be copied. The copy function is similar to this function.  MIDSTR is primarily used to process strings that contain Chinese characters.    Reference example: Var s:string;  h:string; Begin S: = MIDSTR (' China ', 1, 2); s variable is ch H: = midstr (' Computer newspaper ', 1, 1); The h variable is "electricity". If you use the copy function, it should be h: = Copy (' Computer report, 1, 2), otherwise the returned will not be a "power" word.  Therefore, it is best to use midstr when working with strings that contain Chinese. end;  11.Pos Function Description: Find the location of the character (string). The function has two parameters.  The first argument is the character (string) to look for, and the second argument is the searched character (string). Reference example: Var Npos:integer;  The position where the character used to save the lookup begin NPos: = Pos (' Like ', ' I-like reading! ');  End At this point NPOs equals 3.  If it is not found, then NPOs is 0. Note: The POS function is to distinguish the character size when it is found. If you want to implement a non-differentiated size, then you need to use UppercasThe E or lowercase function converts the characters (strings) of two arguments to "uppercase" or "lowercase" and then finds them. There is also a function to find the character (string)----ansipos, which is used exactly like the POS function. When you are looking for kanji, it is best to use the Ansipos function.   12.RIGHTSTR (unit: strutils) function Description: Returns the new character (string) of the specified number to the right of the string. The function has two parameters.  The first parameter is the complete string, and the second parameter is the specified number.  Reference example: Var S, a:string;    Begin S: = ' MSN Messenger '; A: = Rightstr (S, 3); Starting from the far right, get the three characters to the right.  So the A variable equals ger. end;  13.SetLength Function Description: Sets the string or dynamic array length. The function has two parameters.  The first parameter is a string variable or a dynamic array variable to be set, the second parameter is the specified length, and its value range is from 0 to 255.    Reference example: Var s:string; Arrtext:array of Char; Defines a dynamic array begin SetLength (S, 10);    When set, the S variable can only be assigned a string with a value length of 10. SetLength (Arrtext, 10); Dynamic arrays can be used only after allocating memory space for dynamic arrays using SetLength. This code is equivalent to arrtext:array[0..9] of Char end;  14.StrPCopy function Description: Copies the string into a character array. The function has two parameters.  The first parameter is the target array, and the second argument is string. Reference example: Var arrchar:array[0..255] of Char;  A char array of length 256 is declared here to begin Strpcopy (Arrchar, ' Come on, baby! ');  end;  15.Trim Function Description: Delete the left and right sides of the string space (regardless of the left and right side of the number of spaces are all deleted).  Reference example: Var s:string;    Begin S: = ' Delphi 7.0 ';  S: = Trim (s); end;  16.Trimleft function Description: Remove the space to the left of the string (regardless of how many spaces on the left are deleted).  Reference example: S: = Trimleft (' Delphi ');  17.TrimRight function Description: Remove the space to the left of the string (regardless of how many spaces on the left are removed).  Reference example: S: = TrimRight (' Delphi ');  18.UpperCase function Description: Converts the characters (strings) of English characters to uppercase.  Reference example: Var S, a:string;    Begin S: = ' abc '; A: = uppercase (S);  After the uppercase function is converted, A is equal to ABC. end;  three, file, disk operation function software Most of the files, disk operations. Familiarity with these functions can help you easily create, delete, save files and more.   1.Append function Description: Append content to file.  The file must be present.    Reference example: Var s:string;  F:textfile;  Begin S: = ' This was a book. '; AssignFile (F, ' C:\MyFile.txt '); Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later.   Append (F); Open the file Writeln (F, S) in an additional way;    Appends the contents of the S variable to the end of the text. CloseFile (F);  Close file end;  2.AssignFile function Description: Establish a connection to the specified file. Reference example: Var f:textfile; Declaration text file type variable begin AssignFile (F, ' C:\MyFile.txt ');  Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later.  end;  3.ChDir Function Description: Change the current directory (folder).  Reference example: Var sdir:string;    Begin SDir: = ' C:\Windows '; ChDir (SDir); At this point, the current directory of the system is C:\WinDows directory. end;  4.CloseFile Function Description: Close the file.  You should use the CloseFile function to close the open file when the file operation is complete.    Reference example: Var s:string;    alltext:string;  F:textfile; Begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Reset (F); Open file While not EOF (f) DO begin//use while loop, always judging whether the file is Readln (f, S);    Reads a line of text alltext: = Alltext + S;    End CloseFile (F); Close file end;  5.DeleteFile function Description: Deletes the specified file. The function has only one parameter. This parameter is the full path to the file you want to delete. Returns true if the deletion was successful.  Returns false if the deletion fails, or if the file does not exist. Reference example: Var Isok:boolean; Used to save deletion state begin IsOK: = DeleteFile (' C:\My documents\index.html '); After the function is persistent, the result is returned to the isOK variable.    If the isOK variable is true, it indicates that the file was deleted successfully. If IsOK then showmessage (' File deleted successfully! ') Else showmessage (' File delete failed!  ‘); end;  6.DirectoryExists Function Description: Detects whether the specified folder exists.  Returns true if it exists, or false if it is present.  Reference example: Var Isexists:boolean;    Begin isexists: = directoryexists (' C:\Windows '); If Isexists then ShowMessage (' Windows folder exists! ') Else ShowMessage (' Windows folder does not exist!  ‘); end;  7.DiskFree function Description: Gets the left of the specified diskMore space. The function has only one parameter. This parameter is used to specify the disk number to get the remaining space. When the argument is 0 o'clock, it means to get the remaining space of the current disk, 1 is a, 2 is B, and so on.  A return value of 1 indicates that the specified disk is invalid.  Reference example: Var Freesize:int64; Begin Freesize: = Diskfree (3); Gets the remaining space in the C drive.  The value returned is in "bytes". end;  8.DiskSize function Description: Gets the space for the specified disk. The function has only one parameter. This parameter is used to specify the disk number to get disk space. When the argument is 0 o'clock, it means to get the current disk space, 1 is a, 2 is B, and so on.  A return value of 1 indicates that the specified disk is invalid.  Reference example: Var Disksize:int64; Begin disksize:= DiskSize (3); Gets the space of the C drive.  The value returned is in "bytes". end;  9.EOF Function Description: Determine whether the file pointer moved to the end of the file.  When the EOF function returns a value of true, you cannot use the READLN function to read the file.    Reference example: Var s:string;    alltext:string;  F:textfile; Begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Reset (F); Open file While not EOF (f) DO begin//use while loop, always judging whether the file is Readln (f, S);    Reads a line of text alltext: = Alltext + S;  End  end;  10.Erase function Description: Delete files.  Reference example: Var f:file; Begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Reset (F); Open file CloseFile (F); Close file Erase (F); Delete the file.  You must close the file before deleting the file. End;  11.FileExists Function Description: Detects whether the specified file is present.  Returns true if it exists, or false if it is present.  Reference example: Var Isexists:boolean;    Begin isexists: = FileExists (' C:\Test.txt '); If Isexists then ShowMessage (' The file exists! ') Else ShowMessage (' The file does not exist!  ‘); end;  12.FileSize function Description: Gets the file size.  The returned result is a unit of bytes.    Reference example: Var f:file of Byte;  Nsize:longint; Begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Reset (F); Open file NSize: = FileSize (F);    Gets the file size. CloseFile (F); Close file end;  13.ForceDirectories function Description: Create a new subdirectory.  Directories that do not exist in the path are created together. Reference example: Forcedirectories (' C:\Flash\MyFolder '); If the Flash folder itself does not exist, then the Flash folder will be created before creating the MyFolder folder.    14.MkDir Function Description: Create a new subdirectory (folder). Reference example: MkDir (' C:\MyFolder ');  A folder named MyFolder was created in the C packing directory. It is important to note that if you create a subdirectory in a directory that does not exist, it will fail. For example, there is no Flash folder on the C drive, written as mkdir (' C:\Flash\MyFolder '); This will not create the MyFolder folder successfully.    15.Reset Function Description: Open the file as read-only. Reference example: Var f:textfile; Declaration text file type variable begin AssignFile (F, ' C:\MyFile.txt '); Connect the C:\MyFile.txt file to the F variable, and thenPolygons can use the F variable to manipulate the file. Reset (F); Open File end;  16.Rewrite function Description: Open the file in a writable manner. If the file does not exist, it will be created automatically.  With this function, all the contents of the file will be overwritten. Reference example: Var f:textfile; Declaration text file type variable begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Rewrite (F); Open the file. If the file does not exist, the MyFile.txt file will be created in the C drive.  If the file exists, all content in the MyFile.txt will be overwritten.  end;  17.Readln Function Description: Reads a line of text.    Reference example: Var s:string;  F:textfile; Begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Reset (F); Open File Readln (F, S);  Read a line of text into the s variable end;  18.Rename function Description: Change the file name.  Reference example: Var f:file; Begin AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. ReName (F, ' C:\NewFile.txt ');  Renamed to NewFile.txt end;  19.Writeln feature Description: Writes a line of text.    Reference example: Var s:string;  F:textfile;  Begin S: = ' This was a book. '; AssignFile (F, ' C:\MyFile.txt ');    Connect the C:\MyFile.txt file to the F variable, and you can use the F variable to manipulate the file later. Rewrite (F); Create a new file Writeln (F, S);  Writes the contents of the S variable to the text. End;&nbsP Four, memory, pointer operation function in programming, dynamic array for us to deal with the data has brought great convenience. Windows API functions also provide a powerful guarantee for the functionality of the enhanced program. When we use these dynamic arrays and API functions, we often need to dynamically allocate memory space, so that dynamic arrays can be used by us, API functions to correctly return the results. Therefore, these functions are essential.   1.AllocMem function Description: Allocates memory space and automatically initializes to zero.  If you do not need to initialize to zero, you can use GETMEM instead of AllocMem.  Reference example: Var Buffer:pchar; Begin Buffer: = AllocMem (256);  Allocate 256 bytes of memory space end;  2.Dispose function Description: Frees the memory space allocated for the pointer. Reference example: Var P:pinteger;    Declares an integer (int) pointer to begin NEW (P);    p^: = 100;  Dispose (P)//Release memory end;  3.FreeMem function Description: Frees allocated memory space.  Reference example: Var Buffer:pchar; Begin Getmem (Buffer, 256);    Allocates a memory space that is 256 bytes in size. Freemem (Buffer);  Free memory space end;  4.GetMem function Description: Allocates memory space.  Reference example: Var Buffer:pchar; Begin Getmem (Buffer, 256);  Allocates a memory space that is 256 bytes in size.  end;  5.New Function Description: Allocates memory space for the pointer. Reference example: Var P:pinteger; Declares an integer (int) pointer to begin NEW (P); allocating memory p^: = 100; Assignment end;  mathematical arithmetic function we are very useful in writing programs that are closely related to mathematics.  These mathematical arithmetic functions are used in a large number of applications compared to the processing software. By default, the Delphi new project does not contain most of the math run functions, so you need to add the math unit to the uses.   1.Abs Function Description: To find the absolute value.    Reference example: Var r:single;  I:integer; Begin r: = Abs (-2.8); R equals 2.8  I: = Abs (-156);  I equals 156 end;  2.Exp function Description: Exp Returns the value of the X-power of E, where E is a natural logarithmic base. Reference Example: E: = EXP (1.0);  E is the real variable end;  3.Floor function Description: Gets the largest integer less than or equal to X.  Reference example: Floor ( -2.8) =-3 Floor (2.8) = 2 Floor ( -1.0) = -1  4.Int Function Description: Returns the integer part of the parameter.  Reference example: var  r:real; Begin R: = Int (123.456); R equals 123.0 r: = Int (-123.456);  R equals -123.0 end;  5.Max (unit: Math) function Description: Compare two numbers and return the largest number.  Reference example: Var K:integer; Begin K: = Max (10, 20);  K will be the end;  6.Min (unit: Math) function Description: Compare two numbers and return the smallest number.  Reference example: Var K:integer; Begin K: = Min (10, 20);  K will be end;  7.PI function Description: Accurate calculation of return PI.  Reference example: var x Extended; Begin x: = PI;  x equals 3.1415926535897932385 end;  8.Round function Description: Rounding a real number.  Reference example: Var I, J:integer; Begin I: = Round (1.25); I equals 1 J: = Round (1.62);  J equals 2  end;  9.Sqr function Description: Takes the square of the given value. Reference example: Var i:intEger Begin I: = SQR (3); I equals 9 end;  six, date function of date processing, generally in the date-limited sharing, commercial software often used. If you are going to write a date-limited software, be familiar with the following functions.    1.Date Function Description: Returns the current date.  Reference Example: Procedure Tform1.button1click (Sender:tobject);  Begin Label1.Caption: = ' today is: ' + datetostr (Date);  End The label is displayed as: Today is January 1, 2005.    2.DATETOSTR Function Description: Converts a date type to a character type.  Reference example: Var s:string;  Begin S: = Datetostr (Date);  end;  3.DateTimeToStr Function Description: Converts a datetime type to a character type.  Reference example: Var s:string;  Begin S: = Datetimetostr (now);  end;  4.DayOfTheMonth (unit: dateutils) function Description: Gets the day of the specified date.  Reference example: label1.caption: = IntToStr (Dayofthemonth (now)); Assuming the current date is January 2, 2005, the label will be displayed as 2.    5.DayOfTheWeek (unit: dateutils) function Description: Gets the day of the week, based on the specified date.  Reference example: label1.caption: = IntToStr (Dayofthemonth (now)); Assuming the current date is January 2, 2005, the label will be displayed as 7. Judging by the value returned is the number of weeks. 7 means Sunday, 1 is Monday, by analogy.    6.DayOfTheYear (unit: dateutils) function Description: Gets the number of days based on the specified date.  Reference example: label1.caption: = IntToStr (dayOfTheYear (now)); Assuming the current date is January 2, 2005, the label will be displayed as 2. Represents the 2nd day of 2005.   7.DayOf (where the singleMeta: dateutils) function Description: Returns the day according to the specified date.  Reference example: label1.caption: = IntToStr (dayof (Date)); Assuming the current date is January 2, 2005, the label will be displayed as 2.   8.IsLeapYear Function Description: Determines whether a leap year is based on the specified years.  You can use the Yearof function to get the year.  Reference Example: Procedure Tform1.button1click (Sender:tobject);  Begin If Isleapyear (Yearof (Date)) then ShowMessage (' is a leap year ') Else ShowMessage (' Not a leap year ');  end;  9.MonthOf (unit: dateutils) function Description: Returns the month, based on the specified date.  Reference example: label1.caption: = IntToStr (Monthof (Date)); Assuming the current date is January 2, 2005, the label will be displayed as 1.    10.Now Function Description: Returns the current date and time.  Reference Example: Procedure Tform1.button1click (Sender:tobject);  Begin Label1.Caption: = ' now is: ' + datetimetostr;  end;  11.YearOf (unit: dateutils) function Description: Returns the year, based on the specified date.  Reference example: label1.caption: = IntToStr (Yearof (Date)); Assuming the current date is January 2, 2005, the label will be displayed as 2005.        12.formatdatetime (' yyyy m D Day ', DBGRIDEH1. Columns[0].field.value)        formatted date

Delphi 7.0 Common functions Quick Search manual

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.