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 字符串操作函数