Handling strings can be a bit confusing for the Delphi beginner, so here are some helpful tips to get you started.
ShortString is used mainly for backward compatibility with projects made from the Delphi 1 era and it had a limit of 255 charactors.
Examples:
var
StringOne : String;
{255 characters}
StringTwo : String[40];
{ Is a maximum of 40 characters}
The new 32-bit versions of Delphi now have support for Long Strings.
Long Strings have a maximum length that
is limited only by the available memory
Long Strings and Short Strings use 8-bit
Ansi characters and have a length indicator.
IntToStr
StrToInt FloatToStr StrToFloat Val Str UpperCase LowerCase AnsiUpperCase AnsiLowerCase CompareStr CompareText AnsiCompareStr Trim TrimLeft TrimRight QuotedStr IsValidIdent IntToHex StrToIntDef NewStr
|
To convert an integer number to a string.
To convert a string to an integer number. To convert a real number to a string. To convert a string to a real number. To convert a string to a number. To convert any numeric value to a string. Returns a copy of a string in uppercase. Converts an ASCII string to lowercase. Converts a string to upper case. Returns a string that is a copy of the given string converted to lower case. Compares two strings case sensitively. Compares two strings by ordinal value without case sensitivity. Compares strings based on the current Windows locale with case sensitivity. Trims leading and trailing spaces and control characters from a string. Trims leading spaces and control characters from a string. Trims trailing spaces and control characters from a string. Returns the quoted version of a string. Tests for a valid Pascal identifier. Returns the hex representation of an integer. Converts a string that represents an integer (decimal or hex notation) to a number. Allocates a string on the heap. Obsolete.
|
Here are some interesting things
you can
do with strings, labels etc:
Label1.Caption := 'Grace
and ' +
Edit1.Text;
|
procedure
TForm1.Button1Click(Sender:
TObject);
var N : String; begin N := 'Tracy'; Label1.Caption := 'Grace and ' + N; end; Result = Grace and Tracy |
procedure
TForm1.Button1Click(Sender:
TObject);
var I, I2, Result : Integer; begin Result := 0; I := StrToInt(Edit1.Text); I2 := StrToInt(Edit2.Text); Result := I + I2; Label1.Caption := IntToStr(I) + ' + ' + IntToStr(I2) + ' = ' + IntToStr(Result); end; |
|
Editor.Lines.Add('');//Add an empty line to the TMemo
Editor.Lines.Add('Hello world!');//Adds 'Hello world!' to a TMemo
Memo1.Lines.Add(' ' + FormatDateTime('tt ', Now));
|
Here are some
different
ways of using FormatDateTime:
(FormatDateTime('d - m - yy ', Now)); | 16 - 11 - 98 |
(FormatDateTime('dddddd, ' , Now)); | Monday, November 16, 1998, |
(FormatDateTime('mm - dd - yyyy ', Now)); | 11 - 16 - 1998 |
(FormatDateTime('d/mmm/yyyy ', Now)); | 16-Nov-1998 |
|
This code will
add
text to StatusBar panels.
StatusBar1.Panels[0].Text := 'C:\Windows
';
StatusBar1.Panels[1].Text := 'Windows
directory';
|
[procedure
ShowMessage(const Msg: string);]
else
ShowMessage('User Name Not Found');
|
Adding the time
to
a Label with TimeToStr. (Unit SysUtils)
[function
TimeToStr(Time: TDateTime): string;]
Label1.Caption := 'The time is '
+ TimeToStr(Time);
|
Entering the Time
using StrToTime. (Unit SysUtils)
[function
StrToTime(const
S: string): TDateTime;]
procedure TForm1.Button6Click(Sender:
TObject);
var TheTime : TDateTime;
begin
TheTime := StrToTime(Edit1.Text);
Label1.Caption :=
TimeToStr(TheTime);
end;
This code adds
the
full date to a Label. (Unit SysUtils)
[function
FormatDateTime(const Format: string; DateTime: TDateTime): string;]
TheDateLabel.Caption :=
(FormatDateTime('dddddd.
', Now));
|
How to use
IntToStr
and a Label. (Unit SysUtils)
[function IntToStr(Value: Integer): string;] procedure
TForm1.Button4Click(Sender: TObject);
|
|
How to use StrToInt
and a Label. (Unit SysUtils)
procedure
TForm1.Button6Click(Sender: TObject);
|
|
How to use
and a
Label. (Unit SysUtils)
procedure
TForm1.Button6Click(Sender: TObject);
|
|
Label.Caption := Format('%s
%s',
[ExtractFileName(FileName), '']);
|
|
I found that you must use a valid
date
with StrToDate or an error dialog box appears, informing you that the
input
is not a valid date.
procedure
TForm1.Button1Click(Sender: TObject);
Here are some of the examples that I typed into Edit1. 01/03/1999
If you type only 2 numbers into Edit1 like: 1/2 - The label has 1/2/99 - This is because (According to the online help) the 2 numbers are interpreted as m/d or d/m in the current year. Any year numbers (values) between
0 and
99 are assumed to be in the current century.
|
|
|
{Set ColCount to 3} {StringGrid1.Cells[Col, Row]}
{The first 3 lines will display the
column
headings}
StringGrid1.Cells[0, 0] := 'Name:';
StringGrid1.Cells[1, 0] := 'Town:';
StringGrid1.Cells[2, 0] :=
'Country:';
StringGrid1.Cells[0, 1] :=
'James';
{Row 1}
StringGrid1.Cells[1, 1] :=
'Tokoroa';
StringGrid1.Cells[2, 1] := 'New
Zealand';
StringGrid1.Cells[0, 2] :=
'Tracy';
{Row 2}
StringGrid1.Cells[1, 2] :=
'Hamilton';
StringGrid1.Cells[2, 2] := 'USA';
StringGrid1.Cells[0, 3] :=
'Rachel';
{Row 3}
StringGrid1.Cells[1, 3] :=
'Morecambe';
StringGrid1.Cells[2, 3] :=
'England';
|
StrLower converts a string to lowercase.
StrUpper converts a string to uppercase.
[Unit Sysutils]
procedure TForm1.Button5Click(Sender:
TObject);
const S1 : PChar = 'Lowercase and
Uppercase';
begin
Label1.Caption
:= string(StrLower(S1)) + ' && ' + string(StrUpper(S1));
end;
The text in Label1 will be:
"lowercase and uppercase &
LOWERCASE
AND UPPERCASE "
|
If you want to show an '&' in your
Labels etc, then all you have to do is use 2 '&&'.
An example is:
Label1.Caption := 'Come and see Lennies && Gregs Delphi Tips!';
So you will see:
Come and see Lennies & Gregs Delphi Tips!
Instead of:
Come and see Lennies _Gregs Delphi Tips!