UnitSystemCategorystring handling routinesfunction Pos(Substr: string; S: string): Integer;DescriptionPos searches for a substring, Substr, in a string, S. Substr and S are string-type expressions.Pos searches for Substr within S and returns an integer value that is the index of the first character of Substr within S. Pos is case-sensitive. If Substr is not found, Pos returns zero.

解决方案 »

  1.   

    一个例子,希望有帮助!var
      str:String;
      PtPos,len,k:integer;
    begin
      PtPos:=Pos('.',Paytotal);
      len:=length(Paytotal);
      if PtPos<>0 then begin
                         if (PtPos-1)>5 then begin
                           showmessage('超出范围');
                           abort;
                         end;
                         for k:=1 to 5-(PtPos-1) do
                           str:=str+'0';
                           str:=str+StringReplace(Paytotal,'.','',[]);
                         for k:=1 to 4-(len-PtPos) do
                           str:=str+'0';
                       end
        else begin
               if len>5 then begin
                 showmessage('超出范围');
                 abort;
               end;
               for k:=1 to 5-len do
                 str:=str+'0';
                 str:=str+Paytotal+'0000';
             end;
                 result:=str;
    end;
      

  2.   

    function TestTwoString(strFirst : string; strSecond : string; ShowMsg : Boolean): Boolean;
    var
      intLocation : integer;
      strLength   : integer;
      strMessages : string;
    begin
      Result := False;
      if (trim(strFirst) = '') or (trim(strSecond) ='') then
      begin
        MessageBox(Handle, '对不起,要比较的字符不能为空。', '注意',
                   MB_OK + MB_ICONWARNING);
        Exit;
      end;
      strMessages := '字符串“' + strFirst + '”中没有包含字符串“' +
                     strSecond + '”。';
      intLocation := pos(strSecond, strFirst);
      if intLocation > 0 then
      begin
        Result := True;
        strLength := intLocation + Length(strSecond) - 1;
        if Length(strSecond) <2 then
          strMessages := '指定的字符串“' + strSecond +
                         '”存在于字符串“' + strFirst +
                         '”中。' + #13 + '“' + strFirst +
                         '”中的第 ' + inttostr(intLocation) +
                         ' 个字符就是!'
        else
          strMessages := '字符串“' + strFirst +
                         '”中包含字符串“' + strSecond + '”。' +
                         '字符串“' + strFirst + '”中 ' +
                         inttostr(intLocation) + ' 至 ' +
                         inttostr(strLength) + ' 的字符串即是。';
      end;
      if ShowMsg then
        MessageBox(Handle, Pchar(strMessages), '提示',
                   MB_OK + MB_ICONINFORMATION);
    end;//调用例子:      
      TestTwoString('abcdefg','bcd',True);