MyChat Scripts: PosEx, searching a substring in a string with offsett
The function for searching a substring in a string with offset.
Syntax
function PosEx(const sSubSt, sOriginal: string; iOffset: cardinal): integer;
Parameters and return values
Parameter |
Type |
Value |
sSubSt |
string |
what string to search for; |
sOriginal |
string |
source string where the searching performs; |
iOffset |
integer |
offset from which the searching starts; must be >=1. |
Function result
The index of the found substring. If substring is not found or the offset is less or equal to zero, a zero returns. The symbol numeration in the source string starts with 1.
Example
const
ORIGINAL = 'Head high, protest line,' + #13#10 +
'Freedom scribbled on your sign,' + #13#10 +
'Headline New York Times,' + #13#10 +
'Standing on the edge of a revolution.' + #13#10 +
'Hey! Hey! Just obey,' + #13#10 +
'Your secrets safe with the NSA,' + #13#10 +
'In God we trust or the CIA?' + #13#10 +
'Standing on the edge of a revolution.';
FIND = 'revolution';
var
x, iOffset, iCount: integer;
begin
iCount := 0;
iOffset := 1;
mLogScript(ORIGINAL, '');
mLogScript('Finding a word "' + FIND + '"...', '');
repeat
x := PosEx(FIND, ORIGINAL, iOffset);
if x <> 0 then begin
inc(iCount);
iOffset := x + 1;
end;
until x = 0;
mLogScript('Total entries: ' + IntToStr(iCount), '');
end.
Script work result
[11:50:22] (Log "PosEx"): Head high, protest line,
Freedom scribbled on your sign,
Headline New York Times,
Standing on the edge of a revolution.
Hey! Hey! Just obey,
Your secrets safe with the NSA,
In God we trust or the CIA?
Standing on the edge of a revolution.
[11:50:22] (Log "PosEx"): Finding a word "revolution"...
[11:50:22] (Log "PosEx"): Total entries: 2
[11:50:22] (Run "PosEx"): Script operation time: 4 ms
[11:50:22] (Run "PosEx"): Script done successfully.
See also
Created with the Personal Edition of HelpNDoc: Streamline Your CHM Help File Creation with HelpNDoc