Most Important Functions for Strings
Most Important Functions for 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 := '---------';
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);
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';
7. AnsiReplaceStr
function AnsiReplaceStr ( const HayStack, Needle, NewNeedle : string ) : string;
var
haystack : AnsiString;
begin
haystack := 'The big cat sat on the big mat';
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));
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);