Types PAnsiChar and PWideChar. Functions for searching and getting information about strings
Contents
- 1. Function StrEnd. Get a pointer to the last null character of a string
- 2. Function StrLen. Get the length of the string
- 3. Function StrPos. Finding the first occurrence of a substring in a string
- 4. Function StrScan. Search for the first occurrence of a given character in a string
- 5. Function StrRScan. Search for the last occurrence of a given character in a string
- Related topics
Search other resources:
1. Function StrEnd. Get a pointer to the last null character of a string
The StrEnd function returns a pointer to the ‘\0’ character that terminates the string. The function declarations for the PAnsiChar and PWideChar types are as follows
function StrEnd(const Str: PAnsiChar): PAnsiChar; function StrEnd(const Str: PWideChar): PWideChar;
here
- Str – string for which you want to set the pointer to the last character.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; ps2 : PWideChar; begin // Function StrEnd // 1. Source string ps1 := StrNew('Hello, world!'); // 2. Create a new string ps2 := StrNew(ps1); // 3. Copy 5 characters of string ps1 => ps2 StrLCopy(ps2, ps1, 5); // ps2 = 'Hello' // 4. Set the pointer to the last null character of the ps2 string ps3 := StrEnd(ps2); // 5. Add three characters ! StrCat(ps3, '!!!'); // 6. Display the string ps2 Writeln('ps2 = ', StrPas(ps2)); // ps2 = 'Hello!!!' Readln; end.
Result
ps2 = Hello!!!
⇑
2. Function StrLen. Get the length of the string
The StrLen function returns the number of characters in a string, excluding the ‘\0’ character at the end of the string. The function specification has the form
function StrLen(const Str: PAnsiChar): Cardinal; function StrLen(const Str: PWideChar): Cardinal;
here
- Str – the string whose length is to be determined.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PWideChar; length : Integer; begin // Function StrLen - get the length of a string // 1. Specified string ps1 := StrNew('bestprog'); // 2. Determine the length of the string length := StrLen(ps1); // 3. Display the length on the screen Writeln('StrLen(bestprog) = ', length); Readln; end.
Result
StrLen(bestprog) = 8
⇑
3. Function StrPos. Finding the first occurrence of a substring in a string
The StrPos function searches for a substring in a string. The function declaration for PAnsiChar and PWideChar types is as follows
function StrPos(const Str1, Str2: PAnsiChar): PAnsiChar; function StrPos(const Str1, Str2: PWideChar): PWideChar;
here
- Str1 – string in which the search is performed;
- Str2 – substring to be found in the string Str1.
If the substring is found, then the function returns a pointer to the first occurrence of this substring. If the substring is not found, then the function returns nil.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PAnsiChar; ps2 : PAnsiChar; ps3 : PAnsiChar; begin // Function StrPos // 1. Source string ps1 := 'ABCDEF ABCDEFG'; // 2. Source substring ps2 := 'DE'; // 3. Find substring ps2 in string ps1 // 3.1. Вызов StrPos ps3 := StrPos(ps1, ps2); // ps3 => 'DEF ABCDEFG' // 3.2. Display the result if ps3 = nil then Writeln('Substring ps2 is not in the string ps1') else Writeln(StrPas(ps3)); // 4. Find substring 'JKLMN' in string ps1 // 4.1. StrPos call ps3 := StrPos(ps1, 'JKLMN'); // 4.2. Display the result if ps3 = nil then Writeln('Substring ps2 is not in the string ps1') else Writeln(StrPas(ps3)); Readln; end.
Result
DEF ABCDEFG Substring ps2 is not in the string ps1
⇑
4. Function StrScan. Search for the first occurrence of a given character in a string
The StrScan function is used to find the first occurrence of a given character in a string. For PAnsiChar and PWideChar types, the function has the following declaration
function StrScan(const Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; function StrScan(const Str: PWideChar; Chr: WideChar): PWideChar;
here
- Str – string in which the character is searched;
- Chr – a character.
If the character Chr is in the string Str, then the function returns a pointer to the first occurrence of this character in the string. If the character is not found in the string, the function returns nil.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PAnsiChar; ps2 : PAnsiChar; ps3 : PWideChar; ps4 : PWideChar; begin // StrScan function - search for the first occurrence of a given character in a string // 1. String of type PAnsiChar // 1.1. Specify a string of type PAnsiChar ps1 := 'ABCDEF ABCDEFG'; // 1.2. Find the first occurrence of the character 'F' in a string ps2 := StrScan(ps1, 'F'); // 1.3. Display the result if ps2 = nil then Writeln('The character ''F'' is not in the string ps1') else Writeln('ps2 = ', ps2); // output the rest of string // 2. String of PWideChar type // 2.1. Specify a string of type PWideChar ps3 := StrNew('Hello, world!'); // 2.2. Find character 'w' in string ps4 := StrScan(ps3, 'w'); // 2.3. Display the result if ps3 = nil then Writeln('The character ''w'' is not in the string ps1') else Writeln('ps4 = ', ps4); // ps4 = world! // 2.4. Free up memory allocated for ps3 StrDispose(ps3); Readln; end.
Result
ps2 = F ABCDEFG ps4 = world!
⇑
5. Function StrRScan. Search for the last occurrence of a given character in a string
The StrRScan function, like the StrScan function, finds a character in a string. Only the search is performed from the end, that is, the function finds the last occurrence of the given character in the string. The function declaration for PAnsiChar and PWideChar types is as follows
function StrRScan(const Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; function StrRScan(const Str: PWideChar; Chr: WideChar): PWideChar;
here
- Str – string in which the search is performed;
- Chr – character to be searched for.
The function returns a pointer to the position of the found character in the string. If there is no Chr character in the string, the function returns nil.
Example.
program Project1; {$APPTYPE CONSOLE} uses SysUtils; var ps1 : PAnsiChar; ps2 : PAnsiChar; ps3 : PWideChar; ps4 : PWideChar; begin // The StrRScan function searches for the last occurrence of a given character in a string // 1. String of type PAnsiChar // 1.1. Set string of type PAnsiChar ps1 := 'ABCDEF ABCDEFG'; // 1.2. Find last occurrence of character 'A' in string ps2 := StrRScan(ps1, 'A'); // 1.3. Display the result if ps2 = nil then Writeln('The character ''A'' is not in the string ps1') else Writeln('ps2 = ', ps2); // display the rest // 2. String of PWideChar type // 2.1. Set string of type PWideChar ps3 := StrNew('Hello, world!'); // 2.2. Find last occurrence of character 'o' in string ps4 := StrRScan(ps3, 'o'); // 2.3. Display the result if ps3 = nil then Writeln('The character ''o'' is not in the string ps1') else Writeln('ps4 = ', ps4); // ps4 = orld! // 2.4. Free up memory allocated for ps3 StrDispose(ps3); Readln; end.
Result
ps2 = ABCDEFG ps4 = orld!
⇑
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 that change the contents of a string. Functions StrCat, StrLCat, StrLower, StrUpper, StrCopy, StrECopy, StrLCopy, StrMove
- Strings of characters of type String. Operations on character strings
- Functions for working with character strings. Functions Concat, Copy, Delete, Insert, Length, Pos, Val, Str
⇑