Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1326
  • Last Modified:

how to count char

how to count total of specified char in a text.

Say in my memo i have this:

sea is blue, sea near the sea shore. i like the sea

i want to count how many sea and the exist in the text.How

as i do as below
Problem of the code below is that it return line not char


var
text:string;
count : array[sea,the] of integer;
 
count[sea] := 0;
count[the] := 0;
 
text := memo.txt
if AnsiContainsStr(text,'sea') then
showmessage(IntToStr(inc(Count[sea]));
if AnsiContainStr(text,'the') then
showmessage(IntToStr(inc(Count[the]));

Open in new window

0
tankergoblin
Asked:
tankergoblin
  • 3
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
use PosEx to find occurences :
function CountWords(aText, aWord: string): integer;
var n: Integer;
  TempText, TempWord: string;
begin
  Result := 0;
  TempText := UpperCase(aText);
  TempWord := UpperCase(aWord);
  n := 1;
  repeat 
    n := PosEx(TempWord, TempText, n);
    if n > 0 then 
      Result := Result + 1;
  until n = 0;
end;

Open in new window

0
 
tankergoblinAuthor Commented:
what happen if in my text i have this
text = sea sea

the the repeat loop will loop forever

also
example i have text: sea sea

it return 1, why suppose i want it to be 2.
0
 
tankergoblinAuthor Commented:
also i try use this only

n := PosEx(TempWord, TempText, n);

example i have text: sea sea

it return 1, why suppose i want it to be 2.

0
 
Geert GruwezOracle dbaCommented:
i just typed it in the editor, forgot to test it
this is the altered version:
function CountWords(aText, aWord: string): integer;
var n: Integer;
  TempText, TempWord: string;
begin
  Result := 0;
  TempText := UpperCase(aText);
  TempWord := UpperCase(aWord);
  n := 1;
  repeat
    n := PosEx(TempWord, TempText, n);
    if n > 0 then
    begin
      Result := Result + 1;
      n := n +1;
    end;
  until n = 0;
end;

Open in new window

0
 
Geert GruwezOracle dbaCommented:
ps: now i tested it and it works ...
if you want non case-sensitive:
function CountWords(aText, aWord: string; CaseSensitive: boolean = false): integer;
var n: Integer;
  TempText, TempWord: string;
begin
  Result := 0;
  if not CaseSensitive then 
  begin
    TempText := UpperCase(aText);
    TempWord := UpperCase(aWord);
  end 
    else 
  begin
    TempText := aText;
    TempWord := aWord;
  end;
  n := 1;
  repeat
    n := PosEx(TempWord, TempText, n);
    if n > 0 then
    begin
      Result := Result + 1;
      n := n +1;
    end;
  until n = 0;
end;

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now