如何确定一个字符串中某一个字符出现的次数?
有没有具有这样功能的函数?
[解决办法]
POSEX 循环
[解决办法]
没有,要自己写,正好我写过1个,供参考:
- Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject);var t:tstringlist; s:widestring; i,j:integer; s1:string; p:bool;begin s:='asgfhj你好你好吗?'; t:=tstringlist.create; for i:=1 to length(s) do begin s1:=s[i]; p:=false; for j:=0 to t.Count-1 do begin if s1=t.Names[j] then begin p:=true; break; end; end; if p then begin t.Values[s1]:=Inttostr(strtoint(t.Values[s1])+1); end else t.Add(s1+'=1'); end; Memo1.lines:=t; t.Free;end;
[解决办法]
Java里我知道有个方法,算是符合你题意吧。你看看行不?
String str = "1212121";
System.out.println(str.split("1").length);
这里面的1就是你要搜索的字符。分割后得到数组的长度就是这个字符出现的次数。感觉还蛮方便的。
[解决办法]
循环一下就可以了。
[解决办法]
试试这个
function Getchnum(Deststr:string;Findch:char):Integer;
var
Beginnum,Chnum:Integer;
begin
beginnum:=0;
chunm:=0;
while Beginnum<=Length(Deststr) do
begin
if Deststr[Beginnum]=Findch then
Inc(Chnum);
Inc(Beginnum);
end;
Result := Chnum;
end;
[解决办法]
- Delphi(Pascal) code
function GetStrNum(const SubStr, S: string): Integer;var I: Integer;begin Result := 0; I := PosEx(SubStr, S, 1); if I > 0 then Inc(Result) else Exit; while (I > 0) and (I < Length(S)) do begin I := PosEx(SubStr, S, I + 1); if I > 0 then Inc(Result); end;end;ShwMessage(IntToStr(GetStrNum('12', '12312')));