Back


Examples of using Strings/Text with Delphi

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
DisposeStr 
AssignStr
AdjustLineBreaks

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.
Disposes of a string pointer that was allocated using NewStr. Obsolete
Assigns a new dynamically allocated string to a specified pointer. Obsolete
Standardizes line break characters to CR⁄LF. Obsolete.

 



More examples

 
Here are some interesting things you can do with strings, labels etc:

Label1.Caption :=  'Grace and ' + Edit1.Text; 
If Edit1.Text = 'Rachel' then
Result = Grace and Rachel


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;
Adding to a Memo.

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));

FormatDateTime

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

Text on a StatusBar.

This code will add text to StatusBar panels.
StatusBar1.Panels[0].Text := 'C:\Windows ';
StatusBar1.Panels[1].Text := 'Windows directory';
 

Showing a message. (Unit Dialogs)

[procedure ShowMessage(const Msg: string);]
else ShowMessage('User Name Not Found');
 

TimeToStr

Adding the time to a Label with TimeToStr. (Unit SysUtils)
[function TimeToStr(Time: TDateTime): string;]
Label1.Caption := 'The time is  ' + TimeToStr(Time);
 

StrToTime

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));
 

IntToStr
How to use IntToStr and a Label. (Unit SysUtils)
[function IntToStr(Value: Integer): string;]

procedure TForm1.Button4Click(Sender: TObject);
var I : Integer;
begin
  I := 45;
  Label1.Caption := IntToStr(I);
end;
.

StrToInt

How to use StrToInt and a Label. (Unit SysUtils)
[function StrToInt(const S: string): Integer;]

procedure TForm1.Button6Click(Sender: TObject);
var I : Integer; Str : String;
begin
  Str := '321';
  I := StrToInt(Str);
  Label1.Caption := IntToStr(I);
end;
.

StrToIntDef

How to use and a Label. (Unit SysUtils)
[function StrToIntDef(const S: string; Default: Integer): Integer;]

procedure TForm1.Button6Click(Sender: TObject);
var Str : string; I : Integer;
begin
  Str := Edit1.Text;
  I := StrToIntDef(Str, 0); {0 default value}
  Label1.Caption := IntToStr(I);
end;
Note: If you enter a valid number into the edit box then the label will have the same value.
If you type in a non valid integer, then the label will have the default value of 0.
.

Getting the Opened Files Name.

Label.Caption := Format('%s  %s', [ExtractFileName(FileName), '']);
.

StrToDate: (Unit SysUtils)

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. 
Combinations to try: d/m/y, m/d/y and y/m/d.

procedure TForm1.Button1Click(Sender: TObject);
var TheDate : TDateTime;
begin
    TheDate := StrToDate(Edit1.Text);
    Label1.Caption := DateToStr(TheDate);
end;

Here are some of the examples that I typed into Edit1.

01/03/1999
1/3/1999
1/2/2000  - The label had 1/2/00
2/23/2010 - The label had 2/23/10 - I would have preferred 2/23/2010

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. 
See Help for more  information about StrToDate .


Putting text into a StringGrid.

{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';
 

Using StrLower and StrUpper 

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 "
 

Using & (ampersand) in your projects

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!





Back