String Handling Routines: Delphi Programming

man at laptop
Hero Images/Getty Images

The CompareText function compares two strings without case sensitivity.

Declaration:
function
 CompareText(const S1, S2: string): integer;

Description:
Compares two strings without case sensitivity.

The comparison is NOT case sensitive and does not consider the Windows locale settings. The return integer value is less than 0 if S1 is less than S2, 0 if S1 equals S2, or greater than 0 if S1 is greater than S2.

This function is obsolete, i.e. it should not be used in new code - exists only for backward compatibility.

Example:

var s1,s2 : string;
i : integer;
s1:='Delphi';
s2:='Programming';
i:= CompareText(s1,s2);
//i

Copy Function

Returns a substring of a string or a segment of a dynamic array.

Declaration:
function
 Copy(S; Index, Count: Integer): string;
function Copy(S; Index, Count: Integer): array;

Description:
Returns a substring of a string or a segment of a dynamic array.
S is an expression of a string or dynamic-array type. Index and Count are integer-type expressions. Copy returns a string containing a specified number of characters from a string or sub array containing Count elements starting at S[Index].

If Index is greater than the length of S, Copy returns a zero-length string ("") or an empty array. 
If Count specifies more characters or array elements than are available, only the characters or elements from S[Index] to the end of S are returned.

To determine the number of characters in string, use the Length function. A convenient way to copy all the elements of S from the starting Index is to use MaxInt as Count.

Example:

var s : string;
s:='DELPHI';
s := Copy(s,2,3);
//s='ELP';

Delete Procedure

Removes a substring from a string.

Declaration:
procedure
 Delete(var S: string; Index, Count : Integer)

Description:
Removes Count characters from a string S, starting at Index. 
Delphi leaves the string unchanged if Index is not positive or greater than the number of characters after the Index. If Count is greater than the rest of the characters after the Index, the rest of the string is deleted.

Example:

var s : string;
s:='DELPHI';
Delete(s,3,1)
//s=DEPHI;

ExtractStrings Function

Fills a string list with substrings parsed from a delimited list.

Declaration:
type
 TSysCharSet = set of Char;
function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings): Integer;

Description:
Fills a string list with substrings parsed from a delimited list.

Separators are a set of characters that are used as delimiters, separating the substrings, where Carriage returns, newline characters, and quote characters (single or double) are always treated as separators. WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the beginning of a string. Content is the null-terminated string to parse into substrings. Strings is a string list to which all substrings parsed from Content are added. The function returns the number of strings added to the Strings parameter.

Example:

//example 1 - requires TMemo named "Memo1"
ExtractStrings([';',','],
[' '],
'about: delphi; pascal, programming ',
memo1.Lines);
//would result in 3 strings added to memo:
//about: delphi
//pascal
//programming
//example 2
ExtractStrings([DateSeparator], [' '],
PChar(DateToStr(Now)), memo1.Lines);
//would result in 3 strings: day month and year of the currnet date
//for example '06', '25' ,'2003'

LeftStr Function

Returns a string containing a specified number of characters from the left side of a string.

Declaration:
function
 LeftStr(const AString: AnsiString; const Count: Integer): AnsiString;overloadfunction LeftStr(const AString: WideString; const Count: Integer): WideString; overload;

Description:
Returns a string containing a specified number of characters from the left side of a string.

AString represents a string expression from which the leftmost characters are returned. Count indicates how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in AString, the entire string is returned.

Example:

var s : string;
s := 'ABOUT DELPHI PROGRAMMING';
s := LeftStr(s,5);
// s = 'ABOUT'

Length Function

Returns an integer containing the number of characters in a string or the number of elements in an array.

Description:
function
 Length(const S: string): integer
function Length(const S: array): integer

Declaration:
Returns an integer containing the number of characters in a string or the number of elements in an array. 
For an array, Length(S) always returns Ord(High(S))-Ord(Low(S))+1

Example:

var s : string;
i : integer;
s:='DELPHI';
i := Length(s);
//i=6;

LowerCase Function

Returns a string that has been converted to lowercase.

Description:
function
 LowerCase(const S: string): string;

Declaration:
Returns a string that has been converted to lowercase.
LowerCase only converts uppercase letters to lowercase; all lowercase letters and nonletter characters remain unchanged.

Example:

var s : string;
s:='DeLpHi';
s := LowerCase(s);
//s='delphi';

Pos Function

Returns an integer specifying the position of the first occurrence of one string within another.

Declaration:
function
 Pos(Str, Source: string): integer;

Description:
Returns an integer specifying the position of the first occurrence of one string within another.

Pos looks for the first complete occurrence of Str in Source. If it finds one, it returns the character position in Source of the first character in Str as an integer value, otherwise, it returns 0.
Pos is case sensitive.

Example:

var s : string;
i : integer;
s:='DELPHI PROGRAMMING';
i:=Pos('HI PR',s);
//i=5;

PosEx Function

Returns an integer specifying the position of the first occurrence of one string within another, where the search starts at a specified position.

Declaration:
function
 PosEx(Str, Source : string, StartFrom : cardinal = 1): integer;

Description:
Returns an integer specifying the position of the first occurrence of one string within another, where the search starts at a specified position.

PosEx looks for the first complete occurrence of Str in Source, beginning the search at StartFrom. If it finds one, it returns the character position in Source of the first character in Str as an integer value, otherwise, it returns 0. PosEx also returns 0 if StartFrom is greater then Length(Source) or if StartPos is < 0

Example:

var s : string;
i : integer;
s:='DELPHI PROGRAMMING';
i:=PosEx('HI PR', s, 4);
//i=1;

QuotedStr Function

Returns the quoted version of a string.

Declaration:
function
 QuotedStr(const S: string): string;

Description:
Returns the quoted version of a string.

A single quote character (') is inserted at the beginning and end of string S, and each single quote character in the string is repeated.

Example:

var s : string;
s:='Delphi''s Pascal';
//ShowMessage returns Delphi's Pascal
s := QuotedStr(s);
//ShowMessage returns 'Delphi''s Pascal'

ReverseString Function

Returns a string in which the character order of a specified string is reversed.

Declaration:
function
 ReverseString(const AString : string): string;

Description: Returns a string in which the character order of a specified string is reversed

Example:

var s : string;
s:='ABOUT DELPHI PROGRAMMING';
s:=ReverseString(s);
//s='GNIMMARGORP IHPLED TUOBA'

RightStr Function

Returns a string containing a specified number of characters from the right side of a string.

Declaration:
function
 RightStr(const AString: AnsiString; const Count: Integer): AnsiString;overload;
function RightStr(const AString: WideString; const Count: Integer): WideString;overload;

Description:
Returns a string containing a specified number of characters from the right side of a string.

AString represents a string expression from which the rightmost characters are returned. Count indicates how many characters to return. If greater than or equal to the number of characters in AString, the entire string is returned.

Example:

var s : string;
s := 'ABOUT DELPHI PROGRAMMING';
s := RightStr(s,5);
// s = 'MMING'

StringReplace Function

Returns a string in which a specified substring has been replaced with another substring.

Declaration:
type
 TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);

function StringReplace(const S, OldStr, NewStr: string; Flags: TReplaceFlags): string;

Description:
Returns a string in which a specified substring has been replaced with another substring.

If the Flags parameter does not include rfReplaceAll, only the first occurrence of OldStr in S is replaced. Otherwise, all instances of OldStr are replaced by NewStr. 
If the Flags parameter includes rfIgnoreCase, the comparison operation is case insensitive.

Example:

var s : string;
s:='VB programmers love About VB Programming site';
s := ReplaceStr(s,'VB','Delphi', [rfReplaceAll]);
//s='Delphi programmers love
About Delphi Programming site';

Trim Function

Returns a string containing a copy of a specified string without both leading and trailing spaces and control characters.

Declaration: function Trim(const S: string): string;

Description: Returns a string containing a copy of a specified string without both leading and trailing spaces and non-printing control characters.

Example:

var s : string;
s:=' Delphi ';
s := Trim(s);
//s='Delphi';

UpperCase Function

Returns a string that has been converted to uppercase.

Declaration: function UpperCase(const S: string): string;

Description: Returns a string that has been converted to uppercase.
UpperCase only converts lowercase letters to uppercase; all uppercase letters and nonletter characters remain unchanged.

Example:

var s : string;
s:='DeLpHi';
s := UpperCase(s);
//s='DELPHI';

Val Procedure

Converts a string to a numeric value.

Declaration: procedure Val(const S: stringvar Result; var Code: integer);

Description:
Converts a string to a numeric value.

S is a string-type expression; it must be a sequence of characters that form a signed real number. The Result argument can be an Integer or floating-point variable. Code is zero if the conversion is successful. If the string is invalid, the index of the offending character is stored in Code.

Val does not heed the local settings for the decimal separator.

Example:

var s : string;
c,i : integer;
s:='1234';
Val(s,i,c);
//i=1234; //c=0
Format
mla apa chicago
Your Citation
Gajic, Zarko. "String Handling Routines: Delphi Programming." ThoughtCo, Apr. 5, 2023, thoughtco.com/string-handling-routines-delphi-programming-4092534. Gajic, Zarko. (2023, April 5). String Handling Routines: Delphi Programming. Retrieved from https://www.thoughtco.com/string-handling-routines-delphi-programming-4092534 Gajic, Zarko. "String Handling Routines: Delphi Programming." ThoughtCo. https://www.thoughtco.com/string-handling-routines-delphi-programming-4092534 (accessed March 29, 2024).