MyChat Scripts: function StringCharsCount, get the number of the character occurrences in the string
Get the number of occurrences of the specified character in the source string.
Syntax
function StringCharsCount(const s: string; ch: char): integer;
Parameters and return values
Parameter |
Type |
Value |
s |
string |
source string; |
ch |
char |
one character. |
Function result
A number of occurrences of the specified character in the source string. If no occurrences or the string is empty the function returns 0.
Example
const
LYRICS = 'I''m waking up to ash and dust'#13#10 +
'I wipe my brow and I sweat my rust'#13#10 +
'I''m breathing in the chemicals'#13#10 +
'I''m breaking in, shaping up, then checking out on the prison bus'#13#10 +
'This is it, the apocalypse'#13#10 +
'Whoa'#13#10 +
'I''m waking up, I feel it in my bones'#13#10 +
'Enough to make my systems blow'#13#10 +
'Welcome to the new age, to the new age'#13#10 +
'Welcome to the new age, to the new age';
var
sData, s: string;
iCount: integer;
begin
sData := LYRICS;
while length(sData) > 0 do begin
s := Fetch(sData, CRLF);
mLogScript(s, '"i" count = ' + IntToStr(StringCharsCount(LowerCase(s), 'i')));
end;
end.
Script work result
[22:43:33] (Log "StringCharsCount"): ["i" count = 2] I'm waking up to ash and dust
[22:43:33] (Log "StringCharsCount"): ["i" count = 3] I wipe my brow and I sweat my rust
[22:43:33] (Log "StringCharsCount"): ["i" count = 4] I'm breathing in the chemicals
[22:43:33] (Log "StringCharsCount"): ["i" count = 6] I'm breaking in, shaping up, then checking out on the prison bus
[22:43:33] (Log "StringCharsCount"): ["i" count = 3] This is it, the apocalypse
[22:43:33] (Log "StringCharsCount"): ["i" count = 0] Whoa
[22:43:33] (Log "StringCharsCount"): ["i" count = 5] I'm waking up, I feel it in my bones
[22:43:33] (Log "StringCharsCount"): ["i" count = 0] Enough to make my systems blow
[22:43:33] (Log "StringCharsCount"): ["i" count = 0] Welcome to the new age, to the new age
[22:43:33] (Log "StringCharsCount"): ["i" count = 0] Welcome to the new age, to the new age
[22:43:33] (Run "StringCharsCount"): Script operation time: 3 ms
[22:43:33] (Run "StringCharsCount"): Script done successfully.