System.
LStrLAsg() - stores a pointer the string pointed by EDX in memory location
pointed by EAX
Controls.TControl.GetText() - get the text
System.LStrCmp() - compare 2 strings
1. String Copy:
The Copy function has 2 forms. In the first, it creates a new string from part of
an existing string.
In the second, it creates a new array from part of an existing array.
function Copy ( Source : string; StartChar, Count : Integer ) : string;
var
Source, Target : string;
begin
Source := '12345678';
Target := Copy(Source, 3, 4);
ShowMessage('Target : '+Target);
end;
2. Insert string
procedure Insert ( const InsertStr : string; var TargetStr : string; Position :
Integer ) ;
var
Target : string;
begin
Target := '12345678';
Insert('-+-', Target, 3);
ShowMessage('Target : '+Target);
end;
3. StuffString
function StuffString ( const Source : string; Start, Length : Cardinal; const
SubString : string ) : string;
The StuffString function inserts a SubString into another string, replacing Length
characters at position Start.
If the length is -1, then teh string is simply inserted, and does not replace any
characters.
var
source, target : AnsiString;
begin
source := '123456789';
target := StuffString(source, 2, 4, '-inserted-');
ShowMessage('Source = '+source);
ShowMessage('Target = '+target);
end;
4. Move string
procedure Move ( const SourcePointer; var DestinationPointer; CopyCount : Integer )
;
var
source, dest : string;
begin
// Set up our starting string
source := '123456789';
dest := '---------';
// Copy a substring from source into the middle of dest
Move(source[5], dest[3], 4);
5. String concatenate
The Concat function concatenates (joins) strings String1, String2 ... together into
one result string.
function Concat ( const String1 {,String2 ...} : string ) : string;
var
result : string;
begin
// Concatenate using the +
result := 'Hello '+'cruel '+'World';
ShowMessage(result);
// Concatenate using the Concat function
result := Concat('Hello ','cruel ','World');
ShowMessage(result);
end;
6. StringReplace
The StringReplace function replaces the first or all occurences of a substring
OldPattern in SourceString with NewPattern
according to Flags settings.
function StringReplace ( const SourceString, OldPattern, NewPattern : string;
Flags : TReplaceFlags ) : string;
var
before, after : string;
begin
// Try to replace all occurrences of a or A to THE
before := 'This is a way to live A big life';
after := StringReplace(before, ' a ', ' THE ',
[rfReplaceAll, rfIgnoreCase]);
ShowMessage('Before = '+before);
ShowMessage('After = '+after);
end;
7. AnsiReplaceStr
function AnsiReplaceStr ( const HayStack, Needle, NewNeedle : string ) : string;
var
haystack : AnsiString;
begin
haystack := 'The big cat sat on the big mat';
// Note that AnsiReplaceStr is case sensitive
haystack := AnsiReplaceStr(haystack, 'big', 'little');
ShowMessage(haystack); // Display the haystack now
end;
8. String Delete
The Delete procedure deletes up to Count characters from the passed parameter
Source string starting
from position StartChar.
procedure Delete ( var Source : string; StartChar : Integer; Count : Integer ) ;
var
Source : string;
begin
Source := '12345678';
Delete(Source, 3, 4); // Delete the 3rd, 4th, 5th and 6th characters
ShowMessage('Source now : '+Source);
end;
9. StringOfChar
The StringOfChar function creates a new string of length RepeatCount, filled by
RepeatCharacter characters.
function StringOfChar ( RepeatCharacter : Char; RepeatCount : Integer ) : string;
var
text, line : string;
begin
// Write a heading with -'s as underline characters
text := '1.An introduction to life as we know it';
line := StringOfChar('-', Length(text));
// Display our underlined heading
ShowMessage(text);
ShowMessage(line);
end;
10. Trim function which removes spaces:
function Trim ( const Text : String ) : String;
function TrimLeft ( const Text : String ) : String;
function TrimRight ( const Text : String ) : String;
11. Get part of string:
function AnsiLeftStr ( const Source : AnsiString; const Count : Integer ) :
AnsiString;
function AnsiMidStr ( const Source : AnsiString; const Start, Count : Integer ) :
AnsiString;
function AnsiRightStr ( const Source : AnsiString; const Count : Integer ) :
AnsiString;
12. WrapText:
The WrapText function effectively splits a line of text SourceString into multiple
lines in the returned string.
function WrapText ( const SourceString {, MaxColumnSize : Integer = 45} ) : string;
var
before, after : string;
begin
// Set up a long string
before := 'This is quite a long string, at least 50 chars long';
// Split it into multiple lines, each 10 characters long
after := WrapText(before, 10);
// Show the before and after strings
ShowMessage(before);
ShowMessage('');
ShowMessage(after);
end;