我要将银行帐号保留前四位和后四位,中间的用*号代替,有没简单的方法?
如:476861000010201000 要转换成 4768**********1000(位数不一定是18位) 

解决方案 »

  1.   


    function BankStr(Str: string): string;
    var
      TempStr: string;
      Len: integer;
    begin
      Result := '';
      Len := Length(Str)-8;
      if Len<0 then Exit;
      SetLength(TempStr, Len);
      FillChar(TempStr[1], Len, '*');
      Result := Copy(Str, 1, 4) + TempStr + Copy(Str, Len+5, 4);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Showmessage(BankStr('476861000010201000'));
    end;
      

  2.   

    function BankStr(Str: string): string;
    var
      Len: integer;
    begin
      Result := '';
      Len := Length(Str);
      if Len-8<0 then Exit else
        Result := Format('%S'+DupeString('*',Len-8)+'%S',[Copy(Str,1,4),Copy(Str,Len-3,4)]);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Showmessage(BankStr('476861000010201000'));
    end;
      

  3.   

    DupeString函数需要引用StrUtils单元,上面提供另外一个实现
      

  4.   


    function ltostr(s: string): string;
    var
    str,str2:string;
    lstr:string;
    i:integer;
    begin
     str:=Copy(s,5,length(s)-4);
     str2:=Copy(s,Length(s)-3,5);
     for i :=1  to length(s)-8 do
      insert('*',lstr,i);
      Result :=Copy(s,1,4)+lstr+str2;
    end;procedure TForm1.Button1Click(Sender: TObject); 
    begin 
      Showmessage(ltostr('476861000010201000')); 
    end;
      

  5.   


    function BankStr(S: String): String;
    begin
      if Length(S) > 8 then FillChar(S[5], Length(S) - 8, ord('*'));
      Result := S;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Showmessage(BankStr('476861000010201000'));
    end;
      

  6.   


    function ltostr(s: string): string;
    var
    str,str2:string;
    lstr:string;
    i:integer;
    begin
     if Length(s)<8 then exit;
     str:=Copy(s,5,length(s)-4);
     str2:=Copy(s,Length(s)-3,5);
     for i :=1  to length(s)-8 do
      insert('*',lstr,i);
      Result :=Copy(s,1,4)+lstr+str2;
    end;procedure TForm1.Button1Click(Sender: TObject); 
    begin 
      Showmessage(ltostr('476861000010201000')); 
    end;
      

  7.   

    思维活跃是写出优质代码的重要条件。再顶5楼jadeluo秀峰!
      

  8.   

    顶5楼jadeluo秀峰
    function BankStr(S: String): String;
    begin
      Result := FillChar(S[5], Length(S) - 8, ord('*'));
    end;