您的位置:首页 > 编程语言 > Delphi

delphi 封装函数集

2013-10-20 13:51 309 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: