其他分享
首页 > 其他分享> > Delphi 生成随机字符串函数RandomStr

Delphi 生成随机字符串函数RandomStr

作者:互联网

可以生成自定义长度的随机字符串,字符串可以是大小写字母、数字或者是他们的混合。

function RandomStr(StrLength: Integer; Lowercase: Boolean = True; Number: Boolean = True; Uppercase: Boolean = False): string;
const
  UpperStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  LowerStr = 'abcdefghijklmnopqrstuvwxyz';
  NumStr = '0123456789';
var
  SourceStr: string;
  i: Integer;
begin
  SourceStr := '';
  Result := '';
  if Uppercase = True then
    SourceStr := SourceStr + upperStr;
  if Lowercase = True then
    SourceStr := SourceStr + lowerStr;
  if Number = True then
    SourceStr := SourceStr + numStr;
  if (SourceStr = '') or (StrLength < 1) then
    exit;

  Randomize;
  for i := 1 to StrLength do
  begin
    Result := Result + SourceStr[Random(Length(SourceStr) - 1) + 1];
  end;
end;

标签:RandomStr,Delphi,StrLength,Boolean,Result,字符串,SourceStr,True
来源: https://www.cnblogs.com/YXGust/p/16576839.html