计算字符串中某符号的个案,最好是系统中就有的函数。
比如字符串 1,2,3,4,5,6 计算,号的个案为5

解决方案 »

  1.   


    var
      List: TStringList;
    begin
      List := TStringList.Create;
      List.Delimiter := ',';
      List.DelimitedText := '1,2,3,4,5,6';
      ShowMessage(IntToStr(List.Count - 1));  // 5
      List.Free;
    end;
      

  2.   

    你是要计算字符串中某个符号的个数是吧?比如你是计算“,”的个数。如下:
    var
      S :string;
      i,Count :integer;
    begin
      S := '1,2,3,4,5,6';
      Count := 0;
      i := for 1 to Length(S) do
      if S[i]=',' then
         Count := Count + 1;
      showmessage(',个数是:'+ IntToStr(Count));
    end;
        
      

  3.   

    楼主是不是想知道1,2,3,4,5,6字串中出现5的次数啊!不知道我的理解错还是对啊。。如果是上述要求的话,可以扩展TStringList来达到目的!unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TStringlist = class(Classes.TStringList)
      private
        FDuplicateCount:Integer; // 重复个数
        FDuplicateStr:string;   // 需要检查重复的字串
      protected
        function GetDuplicateCount:Integer;
      public
        constructor Create;
        procedure Clear; override;
        function AddObject(const S: string; AObject: TObject): Integer; override;
        property DuplicateCount:Integer read GetDuplicateCount;
      end;  TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public  end;var
      Form1: TForm1;implementationuses RTLConsts;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
      var
      v: TStringList;
      s: string;
    begin
      s := '1,2,国,3,国,4,457,234,国,32,32,国,5667868';
      v := TStringList.Create;
      v.Sorted := True;
      v.Duplicates := dupIgnore;
      v.FDuplicateStr :='国';
      v.Delimiter :=',';
      v.DelimitedText := s;
      ShowMessage(IntToStr(v.DuplicateCount));
      v.Free;
    end;{ TStringlist }function TStringlist.AddObject(const S: string; AObject: TObject): Integer;
    begin
         if not Sorted then
        Result := Count
      else
        if Find(S, Result) then
          case Duplicates of
            dupIgnore:
            begin
              if s = FDuplicateStr then
                Inc(FDuplicateCount);
              Exit;
            end;
            dupError: Error(@SDuplicateString, 0);
          end;
      InsertItem(Result, S, AObject);
    end;procedure TStringlist.Clear;
    begin
      inherited;
      FDuplicateCount := 0;
    end;constructor TStringlist.Create;
    begin
      inherited Create;
    end;function TStringlist.GetDuplicateCount: Integer;
    begin
      Result := 0;
     if FDuplicateCount = 0 then
     begin
       if IndexOf(FDuplicateStr) <> -1 then
         Result := 1;
     end else
     begin
        Result :=  FDuplicateCount + 1;
     end;
    end;end.
      

  4.   

    function GetStrNum(Const Str,SubStr: String): Integer;
    var
      i: Integer;
    begin
      Result:= 0;
      for i:=1 to Length(Str) do
        if Copy(Str,i,Length(SubStr))=SubStr then
          Inc(Result);
    end;
      

  5.   

    貌似系统中没有酱紫的函数
      function GetSubStrCount(const ASubStr, AStr: string): Integer;
      begin
        if ASubStr = '' then
          Result := 0
        else
          Result := (Length(AStr) - Length(StringReplace(AStr, ASubStr, '', [rfReplaceAll]))) div Length(ASubStr)
      end;
      //调用:ShowMessage(IntTOStr(GetSubStrCount(',', '1,2,3,4,5,6 ')))