delphi 封装函数集

1、

// 根据字符串,拆分字符串,相当于vb中的split函数
function SplitString(const Source, ch: string): TStringList;
var
  temp: String;
  i: Integer;
begin
  Result := TStringList.Create;
  // 如果是空自符串则返回空列表
  if Source = '' then
    exit;
  temp := Source;
  i := pos(ch, Source);
  while i <> 0 do
  begin
    Result.add(copy(temp, 0, i - 1));
    Delete(temp, 1, i + length(ch) - 1);
    i := pos(ch, temp);
  end;
  Result.add(temp);
end;

function isSupportFileTypes(ext: string;iniFilePath:string): Boolean;
var
  supportFileTypes: TStringList;
  i: Integer;
  myini:TIniFile;
  config:string;
begin
  Result := False;
  //iniFilePath要绝对路径
  myini:=TIniFile.Create(iniFilePath);
  supportFileTypes := TStringList.Create;
  config:=myini.ReadString('supportExtractFileType','config','');
  supportFileTypes:=SplitString(config,'|');
  // zip apk rar dll exe等不支持抽取文本信息
  for i := 0 to supportFileTypes.Count - 1 do
  begin
    if Trim(supportFileTypes[i]) = LowerCase(ext) then
    begin
      Result := True;
      //ShowMessage('true');
      Break;
    end;
  end;
  supportFileTypes.Free;
  myini.Free;

end;
2、
function TFrmMain.GetHtmlByWebBrowser(const WebBrowser: TWebBrowser;
  URL: string): string;
var
  ms: TMemoryStream;
  MyStrList: TStringList;
begin
  Try
    WebBrowser.Navigate(URL);
  Except

  End;
  while WebBrowser.ReadyState <> READYSTATE_COMPLETE do
    Application.ProcessMessages;
  While not Assigned(WebBrowser.Document) do
    Application.ProcessMessages;
  ms := TMemoryStream.Create;
  MyStrList := TStringList.Create;
  (WebBrowser2.Document as IPersistStreamInit)
  .Save(TStreamAdapter.Create(ms), True);
  ms.Position := 0;
  MyStrList.LoadFromStream(ms, TEncoding.UTF8);
  Result := MyStrList.Text;
end;


3、

function UnicodeToMBCS( // 将Unicode编码字符串转换成多字节字符串
  mCodePage: UINT; // 对照表页码
  mUnicode: WideString // Unicode编码字符串
  ): string; // 返回处理后的字符串
var
  L: Integer;
begin
  L := WideCharToMultiByte(mCodePage, 0, PWideChar(mUnicode), -1, nil, 0, nil,
    nil);
  SetLength(Result, L);
  if L <= 0 then
    Exit;
  WideCharToMultiByte(mCodePage, 0, PWideChar(mUnicode), -1, @Result[1], L,
    nil, nil);
end;

function MBCSToUnicode( // 将多字节字符串转换成Unicode编码字符串
  mCodePage: UINT; // 对照表页码
  mMBCS: string // 多字节字符串
  ): WideString; // 返回处理后的字符串
var
  L: Integer;
begin
  L := MultiByteToWideChar(mCodePage, 0, PAnsiChar(mMBCS), -1, nil, 0);
  SetLength(Result, L);
  if L <= 0 then
    Exit;
  MultiByteToWideChar(mCodePage, 0, PAnsiChar(mMBCS), -1, @Result[1], L);
end;

procedure TForm3.Button1Click(Sender: TObject);
var
  str,str2:string;
begin
  str:=UnicodeToMBCS(CP_UTF8,'你好你是猪test');
  ShowMessage(str);
  str2:=MBCSToUnicode(CP_UTF8,str);
  ShowMessage(str2);
end;

4、提升进程权限:

 procedure TForm1.GetPrivilege;
var
  NewState:       TTokenPrivileges;
  lpLuid:         Int64;
  ReturnLength:   DWord;
  ToKenHandle:    Cardinal;
begin
  OpenProcessToken(GetCurrentProcess,
                   TOKEN_ADJUST_PRIVILEGES
                   OR TOKEN_ALL_ACCESS
                   OR STANDARD_RIGHTS_REQUIRED
                   OR TOKEN_QUERY,ToKenHandle);
  LookupPrivilegeValue(nil,'SeShutdownPrivilege',lpLuid);
  NewState.PrivilegeCount:=1;
  NewState.Privileges[0].Luid:=lpLuid;
  NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
  ReturnLength:=0;
  AdjustTokenPrivileges(ToKenHandle,False,NewState,0,nil,ReturnLength);
end;

5 、stringbuilder

procedure TForm2.Button1Click(Sender: TObject);
var
  sb: TStringBuilder;
begin

  sb := TStringBuilder.Create;
  sb.Append('test');
  sb.AppendLine;//换行
  sb.AppendFormat('%s %d', ['Delphi', 2009]);
  sb.AppendLine;//换行
  sb.AppendLine('[2015-05-04 09:00:03||android0033333][2015-05-04 08:55:28||android003][2015-05-04 08:54:33||android002][2015-05-04 08:53:50||android01][2015-05-04 08:38:02||android]');

  // sb.AppendLine('[2015-6-2 09:19:41||test][2015-6-2 09:21:12||fuck]');
  sb.Replace('[', '');
  sb.Replace(']', '');
  ShowMessage(sb.ToString);
  sb.Free;

end;
6、delphi 字符串操作函数
查找位置函数
[调用格式]:pos(s1,s2);Integer;
[功能]:返回字符串s1在字符串s2中首次出现的位置,若s2中不存在s1,则返回0。
[说明]:  若s1在s2当中,则必须满足s1的所有字符都在s2中,即s1是s2哦子串。
[例如]:  x:=pos('bx','adcd');   //x的值为2
合并字符串过程
[调用格式]
Appendstr(var l;const s2);
[功能]:相同于执行s1:='s1+s2';
[说明]:本过程比语句"s1:=s1+s2";执行效率高
 


截取子字符串函数
[调用格式]: Copy(str, m, n): String;
[功能]:在字符串str 中截取从第 m 个字符开始长度为n 的子字符串,并作为函数的返回值。
[说明]:若m大于s 的长度,则返回一个空串;若从第m 个字符到s 的结尾不足n 个字符,则返回其间的所有字符。
[例如]:
Source code ( By http://www.fishc.com )   
s1:= Copy( ‘IloveFishc!', 6, 5 ); // s1 的值为‘Fishc'
s2:= Copy( ‘小甲鱼爱美眉!’, 4, 5 ); // s2 的值?
s3:= Copy( ‘IloveFishc!’, 1, 1 ); 
s4:= Copy( ‘IloveFishc!’, 0, 1);
注意:s3 和 s4 其实是一样的!
 


删除子字符串过程
[调用格式]:Delete( str, m, n );
[功能]:在字符串str 中删除从第m个字符开始长度为n的子字符串。
[说明]:
如果m大于str 的长度,则不删除任何字符; 
如果从第m 个字符开始到str 的末尾不足n个字符,则删除其间的所有字符; 
如果n小于等于0,则不删除任何字符。 
 


插入子字符串过程
[调用格式]:Insert( str1, str, k );
[功能]:将字符串str1插入到字符串str 中的第k个字符处。


function UpperCase(const S: string): string;          //转大写
function LowerCase(const S: string): string;          //转小写
StringReplace 
SetLength
CopyMemory
GetMem
Move




 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种基于Object Pascal语言的集成开发环境(IDE),用于开发Windows平台的应用程序。在Delphi中,内部函数是指由Delphi提供的一些内置函数,用于执行常见的操作或提供特定功能。以下是一些常用的Delphi内部函数的介绍: 1. Length:用于获取字符串或数组的长度。例如,Length('Hello')将返回5,Length([1, 2, 3])将返回3。 2. Copy:用于复制字符串或数组的一部分。它接受三个参数:源字符串或数组、起始位置和复制长度。例如,Copy('Hello', 2, 3)将返回'ell'。 3. Pos:用于查找子字符串在字符串中的位置。它接受两个参数:子字符串和目标字符串。例如,Pos('lo', 'Hello')将返回4。 4. IntToStr和StrToInt:IntToStr用于将整数转换为字符串,而StrToInt用于将字符串转换为整数。 5. Format:用于格式化字符串。它接受一个格式字符串和一系列参数,并返回一个格式化后的字符串。例如,Format('Hello, %s!', ['John'])将返回'Hello, John!'。 6. DateTimeToStr和StrToDateTime:DateTimeToStr用于将日期时间值转换为字符串,而StrToDateTime用于将字符串转换为日期时间值。 7. Random和RandomRange:Random用于生成一个0到MaxInt之间的随机整数,而RandomRange用于生成一个指定范围内的随机整数。 8. ShowMessage:用于显示一个消息框,其中包含指定的文本。例如,ShowMessage('Hello, World!')将显示一个包含'Hello, World!'文本的消息框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值