About three trim functions
Function prototype functions Trim(const s:string): string; The whitespace and control characters before and after the string are cleared out.
Functional prototype function Trimleft(const s:string): string; Clear whitespace and control characters to the left of the string.
Functional prototype function trimright(const s:string): string; Clear the whitespace and control characters to the right of the string.
Example of trim real-world application scenario
Here's a real-world scenario that uses the Trim function of Delphi, and reading it may give you a deeper understanding of the application of trim in real development
Delphi database application, want to do a user identification of the login window, is to enter the user name and password and then go to the database to verify the user identity, but the program runs, although every time the user name and password entered is correct, but the program always said my password input error, the problem is where? Please look at the code
If Ibt_user_pass. Recordcount=1 thenbegin if Ibt_user_pass. Fieldbyname (' passwd '). Asstring=edit2. Text then //**** form_student_login. Hide Else application. MessageBox (' Please confirm the password is correct! ', ' passwords do not match ', MB_OK); Endelse Application. MessageBox (' Please confirm that the user name is correct! ', ' Without this user ', MB_OK) end;
Only to know that it was added//**** comments that line of code there is a problem, the database if a field is a char type of data, the data will be stored automatically after the string to fill the grid to meet the needs of the number of bits , so on the surface, the obtained password and I entered the same password, is actually not the same.
Workaround:
Option 1:
If Ibt_user_pass. Fieldbyname (' passwd '). asstring = Edit2. Text Then
Switch
Trim (Ibt_user_pass. Fieldbyname (' passwd '). Asstring=edit2. Text Then
Trim function is to remove the extra space in the string
Option 2:
The char type of the database is changed to varchar type
The Trim function of Delphi