Types PAnsiChar, PWideChar. Functions for working with ASCIIZ strings that change the contents of a string
Contents
- 1. Function StrCat. Adding one string to the end of another
- 2. Function StrLCat. Add a string to the end of another string with a given limit on the length of the resulting string
- 3. StrLower and StrUpper functions. Converting characters to upper and lower case
- 4. Function StrCopy. Copying one string to another
- 5. Function StrECopy. Copying one string to another, returning a pointer to the end of the string
- 6. Function StrLCopy. Copy a specified number of characters from one string to another
- 7. Function StrMove. Move one string to another
- Related topics
Search other resources:
1. Function StrCat. Adding one string to the end of another
The StrCat function appends one string to the end of another and returns a pointer to the resulting string. The function has two implementations for PAnsiChar and PWideChar types
function StrCat(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; function StrCat(Dest: PWideChar; const Source: PWideChar): PWideChar;
here
- Dest – destination string. Another string Source is added to this string;
- Source – source string that is added to the end of the Dest string.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PAnsiChar; // Declaring variables of type PAnsiChar ps2 : PAnsiChar; ps3 : PAnsiChar; s : string; begin // 1. Initialize variables ps1, ps2 ps1 := 'bestprog'; ps2 := '.net'; // 2. ps3 = ps1 + ps2 StrCat(ps3, ps1); // ps3 = 'bestprog' StrCat(ps3, ps2); // ps3 = 'bestprog.net' // 3. Output ps3 to console Writeln(StrPas(ps3)); readln; end.
⇑
2. Function StrLCat. Add a string to the end of another string with a given limit on the length of the resulting string
Using the StrLCat function, you can add one string to another with a constraint on the resulting string. For the PAnsiChar and PWideChar types, the function declarations are as follows
function StrLCat(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; function StrLCat(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
here
- Dest – destination string. This is the resulting string to which the Source string is added;
- Source – source string. This string is added to the string Dest;
- MaxLen – maximum length of the resulting string.
The function returns a pointer to the resulting string.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; ps2 : PWideChar; ps3 : PWideChar; begin // Function StrLCat // 1. Form strings ps1, ps2 // 1.1. Reserve 10 characters (20 bytes) of memory ps1 := WideStrAlloc(10); ps2 := WideStrAlloc(10); // 1.2. Copy data to strings StrCopy(ps1, '12345'); StrCopy(ps2, '67890'); // 1.3. Print the strings ps1, ps2 for control Writeln('ps1 = ', StrPas(ps1)); Writeln('ps2 = ', StrPas(ps2)); // 2. Reserve memory for the resulting string ps3 := WideStrAlloc(20); // 3. Add the string ps1 to the resulting string, // string ps3 - 3 characters StrLCat(ps3, ps1, 3); // ps3 = '123' // 4. Add string ps2 to string ps3, // ps3 string max 5 characters StrLCat(ps3, ps2, 5); // ps3 = '12367' // 5. Display ps3 Writeln('ps3 = ', StrPas(ps3)); // 6. Release memory previously allocated // under the lines ps1, ps2, ps3 StrDispose(ps1); StrDispose(ps2); StrDispose(ps3); Readln; end.
Результат
ps1 = 12345 ps2 = 67890 ps3 = 12367
⇑
3. StrLower and StrUpper functions. Converting characters to upper and lower case
The StrLower function converts the characters of a string to lowercase if they are in uppercase. The StrUpper function does the opposite of StrLower and converts the characters in a string to uppercase. The work of both functions applies only to the characters of the Latin alphabet.
For PAnsiChar and PWideChar types, the function declaration is as follows
function StrLower(Str: PAnsiChar): PAnsiChar; function StrLower(Str: PWideChar): PWideChar; function StrUpper(Str: PAnsiChar): PAnsiChar; function StrUpper(Str: PWideChar): PWideChar;
here
- Str is a string whose characters are converted to the appropriate case.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; begin // Functions StrLower, StrUpper // 1. Form string ps1 ps1 := StrNew('ABCdefgh'); Writeln('ps1 = ', StrPas(ps1)); // 2. Convert characters of string ps1 to lowercase StrLower(ps1); Writeln('StrLower(ps1) = ', StrPas(ps1)); // 3. Convert characters to uppercase StrUpper(ps1); Writeln('StrUpper(ps1) = ', StrPas(ps1)); // 4. Free memory allocated for string ps1 StrDispose(ps1); Readln; end.
Result
ps1 = ABCdefgh StrLower(ps1) = abcdefgh StrUpper(ps1) = ABCDEFGH
⇑
4. Function StrCopy. Copying one string to another
The StrCopy function copies the value of one string to another. According to the function declaration documentation for the PAnsiChar and PWideChar types, the following
function StrCopy(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; function StrCopy(Dest: PWideChar; const Source: PWideChar): PWideChar;
here
- Dest – destination string;
- Source – source string. Both strings are located in different areas of memory.
The function returns a pointer to the beginning of the resulting string.
When the function is called, the memory for the source string must be pre-allocated.
Example.
The example demonstrates various operations on strings of the PWideChar type, namely:
- console input and conversion from String type to PWideChar type;
- getting a memory fragment for a String type string (function WideStrAlloc());
- creation of a string of type PWideChar;
- displaying a string of type PWideChar;
- copying strings of type PWideChar using the StrCopy function;
- release of memory allocated for string of PWideChar type.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; ps2 : PWideChar; s : string; begin // Function StrCopy // 1. Create original string, // a string is entered from the keyboard in a console application Write('s = '); Readln(s); // 2. Convert s => ps1 // 2.1. Allocate memory for string ps1, // the memory size is equal to the length of the string s ps1 := StrNew(WideStrAlloc(Length(s))); // 2.2. Copy string s => ps1 StrPCopy(ps1, s); // 3. Allocate memory for string ps2, // the memory size is equal to the length of the string ps1 ps2 := StrNew(ps1); // this is also a copy // 4. Display string ps2 Writeln('ps2 = ', StrPas(ps2)); // 5. Change string ps1 ps1[0] := '+'; ps1[1] := '='; // 6. Print the string ps1 again Writeln('ps1 = ', StrPas(ps1)); // 7. Copy string ps1 to string ps2 - StrCopy function StrCopy(ps2, ps1); // ps2 <= ps1 // 8. Print ps2 string again Writeln('ps2 = ', StrPas(ps2)); // 9. Free memory allocated for strings ps1, ps2 StrDispose(ps1); StrDispose(ps2); Readln; end.
Result
s = 12345 ps2 = 12345 ps1 = +=345 ps2 = +=345
⇑
5. Function StrECopy. Copying one string to another, returning a pointer to the end of the string
The StrECopy function, like the StrCopy function, copies one string to another. The peculiarity of this function is that the pointer is set to the end of the resulting string, and not to the beginning. The function declaration for the PAnsiChar and PWideChar types is as follows
function StrECopy(Dest: PAnsiChar; const Source: PAnsiChar): PAnsiChar; function StrECopy(Dest: PWideChar; const Source: PWideChar): PWideChar;
here
- Dest – destination string;
- Source – source string.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PAnsiChar; ps2 : PAnsiChar; ps3 : PAnsiChar; begin // Function StrECopy // 1. Set string of type PAnsiChar ps1 := AnsiStrAlloc(20); StrCopy(ps1, 'ABCD'); // ps1 = 'ABCD' Writeln('ps1 = ', ps1); // 2. Set the ps2 pointer to the beginning of the string ps1 ps2 := ps1; Writeln('ps2 = ', ps2); // 3. Append to string ps1 the string 'DEF' StrECopy(ps1, 'DEF'); Writeln('StrECopy(ps1, ''DEF'')'); Writeln('ps1 = ', ps1); // ps1 = DEF Writeln('ps2 = ', ps2); // ps2 = DEF - the ps2 pointer has also shifted Readln; end.
Result
ps1 = ABCD ps2 = ABCD StrECopy(ps1, 'DEF') ps1 = DEF ps2 = DEF
⇑
6. Function StrLCopy. Copy a specified number of characters from one string to another
The StrLCopy function is designed to copy a given number of characters from a source string to the resulting string. The function specification for the PAnsiChar and PWideChar types is
function StrLCopy(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; function StrLCopy(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
here
- Dest – destination string;
- Source – source string;
- MaxLen – the number of characters from the string Source to be copied into the string Dest.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; ps2 : PWideChar; begin // Function StrLCopy // Source string ps1 := StrNew('Hello, world!'); // Create a new string ps2 := StrNew(ps1); // Copy first 5 characters of string ps1 => ps2 StrLCopy(ps2, ps1, 5); // ps2 = 'Hello' Writeln('ps1 = ', StrPas(ps1)); Writeln('ps2 = ', StrPas(ps2)); Readln; end.
Result
ps1 = Hello, world! ps2 = Hello
⇑
7. Function StrMove. Move one string to another
The StrMove function moves a sequence of characters from a source string to a destination string. The function declaration for the PAnsiChar and PWideChar types is
function StrMove(Dest: PAnsiChar; const Source: PAnsiChar; Count: Cardinal): PAnsiChar; function StrMove(Dest: PWideChar; const Source: PWideChar; Count: Cardinal): PWideChar;
here
- Dest – destination string;
- Source – source string;
- Count – the number of characters to be copied.
The function returns a pointer to the target string. The function does not destroy the original string Source.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; ps2 : PWideChar; begin // Function StrMove // 1. Form string ps1 ps1 := StrNew('ABCDEFGH'); Writeln('ps1 = ', StrPas(ps1)); // 2. Allocate a piece of memory for a string ps2 // The fragment size is equal to the length of the string ps1 ps2 := WideStrAlloc(StrLen(ps1)); // 3. Transfer data from string ps1, transfer 5 characters. StrMove(ps2, ps1, 5); // 4. Add character '\0' to end of string ps2 ps2[5] := #0; // 4. Display strings Writeln('ps2 = ', StrPas(ps2)); Writeln('ps1 = ', StrPas(ps1)); // 5. Free memory allocated for strings ps1, ps2 StrDispose(ps1); StrDispose(ps2); Readln; end.
Example.
ps1 = ABCDEFGH ps2 = ABCDE ps1 = ABCDEFGH
⇑
Related topics
- Implementation of strings of PChar type in computer memory. Types PAnsiChar, PWideChar. Memory allocation for strings. Access by index
- String compare functions. Functions StrComp, StrIComp, StrLComp, StrLIComp
- Functions for searching and getting information about strings. Functions StrEnd, StrLen, StrPos, StrScan, StrRScan
⇑