yanyyx

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

网上看了一下,好象没有现成的,自己随手写了一下,给大家参考一下吧

 

  1 // a..z  97..122  A..Z  65..90
  2 function UpABC(ABC : String; AddCount : Integer) : String;
  3 var
  4   i, c, x : Integer;
  5   Ask : Boolean;
  6 begin
  7   if Length(ABC) > 0 then
  8   begin
  9     x := AddCount mod 26;
 10 
 11     if Ord(ABC[1]) > 96 then // 判断大小写
 12     begin
 13       Ask := True;
 14       Result := ABC;
 15     end
 16     else
 17     begin
 18       Ask := False;
 19       Result := LowerCase(ABC);
 20     end;
 21 
 22     for i := Length(ABC) downto 1 do
 23     begin
 24       c := Ord(Result[i]);
 25 
 26       if i = Length(Result) then
 27         c := c + x
 28       else
 29         Inc(c);
 30       if c > 122 then  // 超出 z 界
 31       begin
 32         c := c - 26;  // 本位回到 a .. z
 33         Result[i] := Char(c);
 34         if i = 1 then
 35           Result := Char(c) + Result;  // 第一位加 a A
 36       end
 37       else
 38       begin
 39         Result[i] := Char(c);
 40         Break;  // 跳出循环
 41       end;
 42     end;
 43 
 44     if not Ask then
 45       Result := UpperCase(Result);
 46   end
 47   else
 48     Result := '';
 49 end;
 50 
 51 function DownABC(ABC : String; SubCount : Integer) : String;
 52 var
 53   i, c, x : Integer;
 54   Ask : Boolean;
 55 begin
 56   if Length(ABC) > 0 then
 57   begin
 58     x := SubCount mod 26;
 59 
 60     if Ord(ABC[1]) > 96 then // 判断大小写
 61     begin
 62       Ask := True;
 63       Result := ABC;
 64     end
 65     else
 66     begin
 67       Ask := False;
 68       Result := LowerCase(ABC);
 69     end;
 70 
 71     for i := Length(Result) downto 1 do
 72     begin
 73       c := Ord(Result[i]);
 74       if i = Length(Result) then
 75         c := c - x
 76       else
 77         Dec(c);
 78       if c < 97 then
 79       begin
 80         if Length(Result) = 1 then // 长度为 1
 81         begin
 82           Result := 'a';
 83           Break;
 84         end;
 85         c := c + 26;  // 本位回到 z or Z
 86         Result[i] := Char(c);
 87         if i = 1 then
 88           Result := Copy(Result, 1, Length(Result) - 1);
 89       end
 90       else
 91       begin
 92         Result[i] := Char(c);
 93         Break;  // 跳出循环
 94       end;
 95     end;
 96     
 97     if not Ask then
 98       Result := UpperCase(Result);
 99   end
100   else
101     Result := '';
102 end;

 

posted on 2019-04-01 14:28  Yanyyx  阅读(505)  评论(1编辑  收藏  举报