转帖:delphi字符串函数大全 2009-11-17 16:43:55
分类:
delphi字符串函数大全
━━━━━━━━━━━━━━━━━━━━━
首部 function StringToGUID(const S: string): TGUID; $[SysUtils.pas
功能 返回字符串S转换成全局标识
说明 如果字符串非法将触发异常
参考 fuction Windows.Succeeded
例子 Edit2.Text := GUIDToString(StringToGUID(Edit1.Text));
━━━━━━━━━━━━━━━━━━━━━
首部 function GUIDToString(const GUID: TGUID): string; $[SysUtils.pas
功能 返回全局标识GUID转换成字符串
说明 <NULL>
参考 fuction Windows.Succeeded
例子 Edit2.Text := GUIDToString(StringToGUID(Edit1.Text));
━━━━━━━━━━━━━━━━━━━━━
首部 function StringToGUID(const S: string): TGUID; $[SysUtils.pas
功能 返回字符串S转换成全局标识
说明 如果字符串非法将触发异常
参考 fuction Windows.Succeeded
例子 Edit2.Text := GUIDToString(StringToGUID(Edit1.Text));
━━━━━━━━━━━━━━━━━━━━━
首部 function GUIDToString(const GUID: TGUID): string; $[SysUtils.pas
功能 返回全局标识GUID转换成字符串
说明 <NULL>
参考 fuction Windows.Succeeded
例子 Edit2.Text := GUIDToString(StringToGUID(Edit1.Text));
━━━━━━━━━━━━━━━━━━━━━
=================================================
日期与时间函数 (Date and Time Routines)
Unit: SysUtils
========================================
Date 传回今天日期。function Date: TDateTime;
DateTimeToStr 将时间格式转为字串。function DateTimeToStr(DateTime: TDateTime):String;
DateTimeToString 将时间格式转为字串。
procedure DateTimeToString(var Result: string;const Format: string;DateTime: TDateTime);
DateToStr 将日期格式转为字串。function DateToStr(Date: TDateTime): String;
DayOfWeek 传回今天星期几。function DayOfWeek(Date: TDateTime): Integer;
DecodeDate 分解所指定的日期为年、月、日。
procedure DecodeDate(Date: TDateTime;var Year, Month, Day:Word);
DecodeTime 分解所指定的日期为时、分、秒。
procedure DecodeTime(Time: TDateTime;var Hour, Min, Sec,MSec: Word);
EncodeDate 传回将年、月、日所组合的日期格式。
function EncodeDate(Year, Month, Day: Word):TDateTime;
EncodeTime 传回将时、分、秒所组合的时间格式。
function EncodeTime(Hour, Min, Sec, MSec:Word): TDateTime;
FormatDateTime 以指定的格式传回日期时间。
function FormatDateTime(const Format: string;DateTime: TDateTime):String;
Now 传回现在的日期时间。function Now: TDateTime;
StrToDate 将字串转为日期格式。function StrToDate(const S:string): TDateTime;
StrToDateTime 将字串转为日期时间格式function StrToDateTime(const S: string): TDateTime;
StrToTime 将字串转为时间格式。function StrToTime(const S:string): TDateTime;
Time 传回现在时间。function Time: TDateTime;
TimeToStr 将时格式转为字串。function TimeToStr(Time:TDateTime): String;
========================================
档案管理函数
Unit: SysUtils
========================================
ChangeFileExt 变更档案的延伸档名。
function ChangeFileExt(const FileName,Extension: string):string;
DateTimeToFileDate 将Delphi 的日期格式转换为DOS的日期格式。
functionDateTimeToFileDate(DateTime:TDateTime): Longint;
DeleteFile 删除一个档案。
function DeleteFile(const FileName: string):Boolean;
DiskFree 传回磁碟的可用空间。function DiskFree(Drive: Byte): Longint;
DiskSize 传回指定磁碟的容量大小。function DiskSize(Drive: Byte): Longint;
ExpandFileName 传回一个完整的路径及档名字串。
function expandFileName(const FileName: string):string;
ExtractFileExt 传回档案的延伸档名。function ExtractFileExt(const FileName string):string;
ExtractFileName 传回档案的档名。function ExtractFileName(const FileName: string):string;
ExtractFilePath 传回档案的路径。function ExtractFilePath(const FileName: string):string;
FileAge 传回档案的年纪function FileAge(const FileName: string):Longint;
FileCreate 以指定档名建立一个档案。function FileCreate(const FileName: string):Integer;
FileClose 关闭指定的档案。procedureFileClose(Handle: Integer);
FileDateToDateTime 将DOS 的日期格式转为Delphi 的日期格式。
function FileDateToDateTime(FileDate: Longint):TDateTime;
FileExists 判别档案是否存在。function FileExists(const FileName: string):Boolean;
FileGetAttr 传回档案属性。function FileGetAttr(const FileName: string):Integer;
FileGetDate 传回档案的日期及时间。function FileGetDate(Handle: Integer): Longint;
FileRead 从指定的档案读入资料。
function FileRead(Handle:Integer; var Buffer;Count:Longint):Longint;
FileSearch 在目录列中搜寻指定的档案。function FileSearch(constName, DirList: string):string;
FileSeek 改变档案游标的位置。function FileSeek(Handle: Integer;Offset: Longint;Origin: Integer):Longint;
FileSetAttr 设定档案属性。function FileSetAttr(const FileName: string;Attr:Integer): Integer;
FileSetDate 设定档案的日期及时间。procedure FileSetDate(Handle: Integer; Age:Longint);
FileOpen 开启档案。function FileOpen(const FileName: string; Mode:Word): Integer;
FileWrite 写资料到档案。function FileWrite(Handle:Integer;const Buffer; Count:Longint): Longint;
FindClose 终止找寻第一个/下一个的动作。procedure FindClose(var SearchRec: TSearchRec);
FindFirst 寻找第一个符合的档案并设定其属性。
function FindFirst(const Path: string;Attr: Word; var F:TSearchRec): Integer;
FindNext 传回下一个符合的档案。function FindNext(var F:TSearchRec): Integer;
RenameFile 变更档名。function RenameFile(const OldName,NewName: string):Boolean;
日期与时间函数 (Date and Time Routines)
Unit: SysUtils
========================================
Date 传回今天日期。function Date: TDateTime;
DateTimeToStr 将时间格式转为字串。function DateTimeToStr(DateTime: TDateTime):String;
DateTimeToString 将时间格式转为字串。
procedure DateTimeToString(var Result: string;const Format: string;DateTime: TDateTime);
DateToStr 将日期格式转为字串。function DateToStr(Date: TDateTime): String;
DayOfWeek 传回今天星期几。function DayOfWeek(Date: TDateTime): Integer;
DecodeDate 分解所指定的日期为年、月、日。
procedure DecodeDate(Date: TDateTime;var Year, Month, Day:Word);
DecodeTime 分解所指定的日期为时、分、秒。
procedure DecodeTime(Time: TDateTime;var Hour, Min, Sec,MSec: Word);
EncodeDate 传回将年、月、日所组合的日期格式。
function EncodeDate(Year, Month, Day: Word):TDateTime;
EncodeTime 传回将时、分、秒所组合的时间格式。
function EncodeTime(Hour, Min, Sec, MSec:Word): TDateTime;
FormatDateTime 以指定的格式传回日期时间。
function FormatDateTime(const Format: string;DateTime: TDateTime):String;
Now 传回现在的日期时间。function Now: TDateTime;
StrToDate 将字串转为日期格式。function StrToDate(const S:string): TDateTime;
StrToDateTime 将字串转为日期时间格式function StrToDateTime(const S: string): TDateTime;
StrToTime 将字串转为时间格式。function StrToTime(const S:string): TDateTime;
Time 传回现在时间。function Time: TDateTime;
TimeToStr 将时格式转为字串。function TimeToStr(Time:TDateTime): String;
========================================
档案管理函数
Unit: SysUtils
========================================
ChangeFileExt 变更档案的延伸档名。
function ChangeFileExt(const FileName,Extension: string):string;
DateTimeToFileDate 将Delphi 的日期格式转换为DOS的日期格式。
functionDateTimeToFileDate(DateTime:TDateTime): Longint;
DeleteFile 删除一个档案。
function DeleteFile(const FileName: string):Boolean;
DiskFree 传回磁碟的可用空间。function DiskFree(Drive: Byte): Longint;
DiskSize 传回指定磁碟的容量大小。function DiskSize(Drive: Byte): Longint;
ExpandFileName 传回一个完整的路径及档名字串。
function expandFileName(const FileName: string):string;
ExtractFileExt 传回档案的延伸档名。function ExtractFileExt(const FileName string):string;
ExtractFileName 传回档案的档名。function ExtractFileName(const FileName: string):string;
ExtractFilePath 传回档案的路径。function ExtractFilePath(const FileName: string):string;
FileAge 传回档案的年纪function FileAge(const FileName: string):Longint;
FileCreate 以指定档名建立一个档案。function FileCreate(const FileName: string):Integer;
FileClose 关闭指定的档案。procedureFileClose(Handle: Integer);
FileDateToDateTime 将DOS 的日期格式转为Delphi 的日期格式。
function FileDateToDateTime(FileDate: Longint):TDateTime;
FileExists 判别档案是否存在。function FileExists(const FileName: string):Boolean;
FileGetAttr 传回档案属性。function FileGetAttr(const FileName: string):Integer;
FileGetDate 传回档案的日期及时间。function FileGetDate(Handle: Integer): Longint;
FileRead 从指定的档案读入资料。
function FileRead(Handle:Integer; var Buffer;Count:Longint):Longint;
FileSearch 在目录列中搜寻指定的档案。function FileSearch(constName, DirList: string):string;
FileSeek 改变档案游标的位置。function FileSeek(Handle: Integer;Offset: Longint;Origin: Integer):Longint;
FileSetAttr 设定档案属性。function FileSetAttr(const FileName: string;Attr:Integer): Integer;
FileSetDate 设定档案的日期及时间。procedure FileSetDate(Handle: Integer; Age:Longint);
FileOpen 开启档案。function FileOpen(const FileName: string; Mode:Word): Integer;
FileWrite 写资料到档案。function FileWrite(Handle:Integer;const Buffer; Count:Longint): Longint;
FindClose 终止找寻第一个/下一个的动作。procedure FindClose(var SearchRec: TSearchRec);
FindFirst 寻找第一个符合的档案并设定其属性。
function FindFirst(const Path: string;Attr: Word; var F:TSearchRec): Integer;
FindNext 传回下一个符合的档案。function FindNext(var F:TSearchRec): Integer;
RenameFile 变更档名。function RenameFile(const OldName,NewName: string):Boolean;
怎样在 CASE 语句中使用字符串
/////////////////////////////////////////
/////////////////////////////////////////
首先建立 CaseString 函数,用于获取某字符串在一个字符串数组中的顺序:
function CaseString (const s: string;
const x: array of string): Integer;
var i: Integer;
begin
Result:= -1; // Default return parameter
for i:= Low (x) to High (x) do begin
if s = x[i] then begin Result:= i; Exit; end;
end;
end;
const x: array of string): Integer;
var i: Integer;
begin
Result:= -1; // Default return parameter
for i:= Low (x) to High (x) do begin
if s = x[i] then begin Result:= i; Exit; end;
end;
end;
Low() 提供第一个数组成员(通常是0),High() 则返回最后一个。因为 CaseString 返回的是待查字符串在字符串数组中的位置,因此,它可以被直接用到 CASE 语句中:
search:= 'delphi3000';
case CaseString (search, ['delphi3000',
'delphipages',
'Torry's']) of
0: s:= 'Excellent!';
1: s:= 'Good source';
2: s:= 'Not bad!';
end;
case CaseString (search, ['delphi3000',
'delphipages',
'Torry's']) of
0: s:= 'Excellent!';
1: s:= 'Good source';
2: s:= 'Not bad!';
end;
copy(str,pos,num) 从str字符串的pos处开始,截取num个字符的串返回.假设str为'abcdef',copy(str,3,2)='cd',copy(str,4,10)='def'
concat(str1,str2{,strn}) 把各自变量连接起来,返回连接后的字符串(长度不能超过255)
concat(str1,str2{,strn}) 把各自变量连接起来,返回连接后的字符串(长度不能超过255)
length(str) 返回str的字符个数,即其长度.
pos(obj,target) 在target字符串中找出第一个出现obj的第一个字符位置,如果找不到,返回0.
AnsiStrLastChar('你好')结果是“好”。如果有半个汉字出现,返回这半个汉字。二者字符串长度分别为2和1。
CompareStr - 区分大小写
CompareText - 不区分大小写
CompareText - 不区分大小写
////////////////////////////////////////////////////////
{判断字符是否是数字}
function IsDigit(ch: char): boolean;
begin
Result := ch in ['0'..'9'];
end;
{判断字符是否是大写字符}
function IsUpper(ch: char): boolean;
begin
Result := ch in ['A'..'Z'];
end;
{判断字符是否是数字}
function IsDigit(ch: char): boolean;
begin
Result := ch in ['0'..'9'];
end;
{判断字符是否是大写字符}
function IsUpper(ch: char): boolean;
begin
Result := ch in ['A'..'Z'];
end;
{判断字符是否是小写字符}
function IsLower(ch: char): boolean;
begin
Result := ch in ['a'..'z'];
end;
function IsLower(ch: char): boolean;
begin
Result := ch in ['a'..'z'];
end;
{转换为大写字符}
function ToUpper(ch: char): char;
begin
Result := chr(ord(ch) and $DF);
end;
function ToUpper(ch: char): char;
begin
Result := chr(ord(ch) and $DF);
end;
{转换为小写字符}
function ToLower(ch: char): char;
begin
Result := chr(ord(ch) or $20);
end;
function ToLower(ch: char): char;
begin
Result := chr(ord(ch) or $20);
end;
{ Capitalizes first letter of every word in s }
function Proper(const s: string): string;
var
i: Integer;
CapitalizeNextLetter: Boolean;
begin
Result := LowerCase(s);
CapitalizeNextLetter := True;
for i := 1 to Length(Result) do
begin
if CapitalizeNextLetter and IsLower(Result[i]) then
Result[i] := ToUpper(Result[i]);
CapitalizeNextLetter := Result[i] = ' ';
end;
end;
////////////////////////////////////////////////////////////
{返回两个子字符串之间字符的个数}
Function p2pcount( s, ss1, ss2 : string ): integer;
var i, j, slen : integer;
begin
i := pos( ss1, s );
j := pos( ss2, s );
slen := Length(ss2);
if j >= i then Result := j - i + slen else Result := 0;
end;
function Proper(const s: string): string;
var
i: Integer;
CapitalizeNextLetter: Boolean;
begin
Result := LowerCase(s);
CapitalizeNextLetter := True;
for i := 1 to Length(Result) do
begin
if CapitalizeNextLetter and IsLower(Result[i]) then
Result[i] := ToUpper(Result[i]);
CapitalizeNextLetter := Result[i] = ' ';
end;
end;
////////////////////////////////////////////////////////////
{返回两个子字符串之间字符的个数}
Function p2pcount( s, ss1, ss2 : string ): integer;
var i, j, slen : integer;
begin
i := pos( ss1, s );
j := pos( ss2, s );
slen := Length(ss2);
if j >= i then Result := j - i + slen else Result := 0;
end;
{更快速的字符查询,快40%}
function ScanStr(ToScan: PChar; Sign: Char):PChar;
begin
Result:= nil;
if ToScan <> nil then
while (ToScan^ <> #0) do begin
if ToScan^ = Sign then begin
Result:= ToScan;
break;
end;
inc(ToScan);
end;
end;
/////////////////////////////
替换字符串中子串的函数,他可以从字符串中找出指定子串,并替换为另一子串。
function replacing(S,source,target:string):string;
var site,StrLen:integer;
begin
{source在S中出现的位置}
site:=pos(source,s);
function ScanStr(ToScan: PChar; Sign: Char):PChar;
begin
Result:= nil;
if ToScan <> nil then
while (ToScan^ <> #0) do begin
if ToScan^ = Sign then begin
Result:= ToScan;
break;
end;
inc(ToScan);
end;
end;
/////////////////////////////
替换字符串中子串的函数,他可以从字符串中找出指定子串,并替换为另一子串。
function replacing(S,source,target:string):string;
var site,StrLen:integer;
begin
{source在S中出现的位置}
site:=pos(source,s);
{source的长度}
StrLen:=length(source);
StrLen:=length(source);
{删除source字符串}
delete(s,site,StrLen);
delete(s,site,StrLen);
{插入target字符串到S中}
insert(target,s,site);
insert(target,s,site);
{返回新串}
replacing:=s;
end;
///////////////////////
另两个替换字符串中子串的函数
function repl_substr( sub1, sub2, s: string ): string;
var i: integer;
begin
repeat
i := pos( sub1, s ) ;
if i > 0 then begin
delete( s, i, Length(sub1));
insert( sub2, s, i );
end;
until i < 1;
Result := s;
end;
replacing:=s;
end;
///////////////////////
另两个替换字符串中子串的函数
function repl_substr( sub1, sub2, s: string ): string;
var i: integer;
begin
repeat
i := pos( sub1, s ) ;
if i > 0 then begin
delete( s, i, Length(sub1));
insert( sub2, s, i );
end;
until i < 1;
Result := s;
end;
function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String;
Var Position: Integer;
TempStr: String;
begin
Position := Pos(ReplacePiece,S);
if Position > 0 then Begin
TempStr := S;
Delete(TempStr,1,Position-1+Length(ReplacePiece));
Result :=
Copy(S,1,Position-1)+ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith)
TempStr: String;
begin
Position := Pos(ReplacePiece,S);
if Position > 0 then Begin
TempStr := S;
Delete(TempStr,1,Position-1+Length(ReplacePiece));
Result :=
Copy(S,1,Position-1)+ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith)
End else Result := S;
end;
////////////////////////
替换全部子字符串的函数
end;
////////////////////////
替换全部子字符串的函数
function ReplaceSub(str, sub1, sub2: String): String;
var
aPos: Integer;
rslt: String;
var
aPos: Integer;
rslt: String;
begin
aPos := Pos(sub1, str);
rslt := '';
while (aPos <> 0) do begin
rslt := rslt + Copy(str, 1, aPos - 1) + sub2;
Delete(str, 1, aPos + Length(sub1));
aPos := Pos(sub1, str);
end;
Result := rslt + str;
end;
aPos := Pos(sub1, str);
rslt := '';
while (aPos <> 0) do begin
rslt := rslt + Copy(str, 1, aPos - 1) + sub2;
Delete(str, 1, aPos + Length(sub1));
aPos := Pos(sub1, str);
end;
Result := rslt + str;
end;
/////////////////////////
在字符串左右填充指定数量的指定字符
function UT_PadString(inString :string; maxLength :integer; padChar :char;
left :boolean) :string;
begin
result := inString;
while (Length(result) < maxLength) do
if (left) then
result := padChar + result
else
result := result + padChar;
end;
/////////////////////////////////////
提取字符串中指定子字符串前的字符串
Function Before ( Src:string ; Var S:string ) : string ;
Var
F : Word ;
在字符串左右填充指定数量的指定字符
function UT_PadString(inString :string; maxLength :integer; padChar :char;
left :boolean) :string;
begin
result := inString;
while (Length(result) < maxLength) do
if (left) then
result := padChar + result
else
result := result + padChar;
end;
/////////////////////////////////////
提取字符串中指定子字符串前的字符串
Function Before ( Src:string ; Var S:string ) : string ;
Var
F : Word ;
begin
F := POS (Src,S) ;
if F=0 then
Before := S
else
Before := COPY(S,1,F-1) ;
end ;
//////////////////////////////////
提取字符串中指定子字符串后的字符串
Function After ( Src:string ; Var S:string ) : string ;
Var
F : Word ;
F := POS (Src,S) ;
if F=0 then
Before := S
else
Before := COPY(S,1,F-1) ;
end ;
//////////////////////////////////
提取字符串中指定子字符串后的字符串
Function After ( Src:string ; Var S:string ) : string ;
Var
F : Word ;
begin
F := POS (Src,S) ;
if F=0 then
After := ''
else
After := COPY(S,F+length(src),length(s)) ;
end ;
////////////////////////////////////
判断字符串是否可以转换为整数
function IsIntStr(const S: string): boolean;
begin
Result:=StrToIntDef(S,0)=StrToIntDef(S,1);
end;
//////////////////////////////////////
从字符串中删除指定字符串
procedure RemoveInvalid(what, where: string): string;
F := POS (Src,S) ;
if F=0 then
After := ''
else
After := COPY(S,F+length(src),length(s)) ;
end ;
////////////////////////////////////
判断字符串是否可以转换为整数
function IsIntStr(const S: string): boolean;
begin
Result:=StrToIntDef(S,0)=StrToIntDef(S,1);
end;
//////////////////////////////////////
从字符串中删除指定字符串
procedure RemoveInvalid(what, where: string): string;
var
tstr: string;
tstr: string;
begin
tstr:=where;
while pos(what, tstr)>0 do
tstr:=copy(tstr,1,pos(what,tstr)-1) +
copy(tstr,pos(what,tstr)+length(tstr),length(tstr));
Result:=tstr;
end;
用法:
NewStr:=RemoveInvalid('<invalid>','This <invalid> is my string and I wan to
remove the word <invalid>');
///////////////////////////////////////////
根据某个字符分割字符串的函数
procedure SeparateTerms(s : string;Separator : char;Terms : TStringList);
{ This browses a string and divide it into terms whenever the given
separator is found. The separators will be removed }
var
hs : string;
p : integer;
tstr:=where;
while pos(what, tstr)>0 do
tstr:=copy(tstr,1,pos(what,tstr)-1) +
copy(tstr,pos(what,tstr)+length(tstr),length(tstr));
Result:=tstr;
end;
用法:
NewStr:=RemoveInvalid('<invalid>','This <invalid> is my string and I wan to
remove the word <invalid>');
///////////////////////////////////////////
根据某个字符分割字符串的函数
procedure SeparateTerms(s : string;Separator : char;Terms : TStringList);
{ This browses a string and divide it into terms whenever the given
separator is found. The separators will be removed }
var
hs : string;
p : integer;
begin
Terms.Clear; // First remove all remaining terms
if Length(s)=0 then // Nothin' to separate
Exit;
p:=Pos(Separator,s);
while P<>0 do
begin
hs:=Copy(s,1,p-1); // Copy term
Terms.Add(hs); // Add to list
Delete(s,1,p); // Remove term and separator
p:=Pos(Separator,s); // Search next separator
end;
if Length(s)>0 then
Terms.Add(s); // Add remaining term
end;
==========
= 用 法
==========
Terms.Clear; // First remove all remaining terms
if Length(s)=0 then // Nothin' to separate
Exit;
p:=Pos(Separator,s);
while P<>0 do
begin
hs:=Copy(s,1,p-1); // Copy term
Terms.Add(hs); // Add to list
Delete(s,1,p); // Remove term and separator
p:=Pos(Separator,s); // Search next separator
end;
if Length(s)>0 then
Terms.Add(s); // Add remaining term
end;
==========
= 用 法
==========
var
Terms : TStringList;
i : integer;
Terms : TStringList;
i : integer;
const
TestStr = '1st term;2nd term;3rd term';
TestStr = '1st term;2nd term;3rd term';
begin
Terms:=TStringList.Create;
SeparateTerms(TestStr,';',Terms);
for i:=0 to terms.Count-1 do
ShowMessage(Terms.Strings[i]);
Terms.Free;
end;
/////////////////////////////
根据一组字符分割字符串的函数
type
Charset = set of Char;
var
f : Text;
s : String;
Terms:=TStringList.Create;
SeparateTerms(TestStr,';',Terms);
for i:=0 to terms.Count-1 do
ShowMessage(Terms.Strings[i]);
Terms.Free;
end;
/////////////////////////////
根据一组字符分割字符串的函数
type
Charset = set of Char;
var
f : Text;
s : String;
procedure WriteStringSplitted(var s: String; Separators: Charset);
var
a,e : Integer; {anfang und ende des w鰎tchens}
begin
a := 1;
for e := 1 to Length(s) do
if s[e] in Separators then begin
WriteLn(Copy(s, a, e-a));
a := e + 1;
end;
WriteLn(Copy(s, a, e-a+1));
end;
var
a,e : Integer; {anfang und ende des w鰎tchens}
begin
a := 1;
for e := 1 to Length(s) do
if s[e] in Separators then begin
WriteLn(Copy(s, a, e-a));
a := e + 1;
end;
WriteLn(Copy(s, a, e-a+1));
end;
begin
Assign(f, 'c:\dingsbums\text.txt');
Reset(f);
while not EOF(f) do begin
ReadLn(f,s);
WriteStringSplitted(s, [':', ',']);
end;
Close(f);
end.
//////////////////////////////////////////////////
Assign(f, 'c:\dingsbums\text.txt');
Reset(f);
while not EOF(f) do begin
ReadLn(f,s);
WriteStringSplitted(s, [':', ',']);
end;
Close(f);
end.
//////////////////////////////////////////////////
{===============================================================}
{ 函数 : RESULTSTRING = HexToBin(HEXSTRING)
{ 目的 : 把十六进制字符串转换为二进制字符串
{
{===============================================================}
{ 函数 : RESULTINTEGER = HexCharToInt(HEXCHAR)
{ 目的 : 转换一个十六进制字符为整数
{===============================================================}
{ 函数 : RESULTSTRING = HexCharToBin(HEXCHAR)
{ 目的 : 转换一个十六进制字符为二进制字符串
{===============================================================}
{ 函数 : RESULTINTEGER = Pow(BASE,POWER)
{ 目的 : 指数函数
{===============================================================}
{ 函数 : RESULTINTEGER = BinStrToInt(BINSTRING)
{ 目的 : 把二进制字符串转换为整数
{===============================================================}
{ 函数 : RESULTSTRING = DecodeSMS7Bit (PDUSTRING)
{ 目的 : 解码一个7-bit SMS (GSM 03.38) 为ASCII码
{===============================================================}
{ 函数 : RESULTSTRING = ReverseStr (SOURCESTRING)
{ 目的 : 反转一个字符串
{===============================================================}
{ 函数 : RESULTSTRING = HexToBin(HEXSTRING)
{ 目的 : 把十六进制字符串转换为二进制字符串
{
{===============================================================}
{ 函数 : RESULTINTEGER = HexCharToInt(HEXCHAR)
{ 目的 : 转换一个十六进制字符为整数
{===============================================================}
{ 函数 : RESULTSTRING = HexCharToBin(HEXCHAR)
{ 目的 : 转换一个十六进制字符为二进制字符串
{===============================================================}
{ 函数 : RESULTINTEGER = Pow(BASE,POWER)
{ 目的 : 指数函数
{===============================================================}
{ 函数 : RESULTINTEGER = BinStrToInt(BINSTRING)
{ 目的 : 把二进制字符串转换为整数
{===============================================================}
{ 函数 : RESULTSTRING = DecodeSMS7Bit (PDUSTRING)
{ 目的 : 解码一个7-bit SMS (GSM 03.38) 为ASCII码
{===============================================================}
{ 函数 : RESULTSTRING = ReverseStr (SOURCESTRING)
{ 目的 : 反转一个字符串
{===============================================================}
unit BinHexTools;
interface
interface
function HexToBin(HexNr : string): string;
function HexCharToInt(HexToken : char):Integer;
function HexCharToBin(HexToken : char): string;
function pow(base, power: integer): integer;
function BinStrToInt(BinStr : string) : integer;
function DecodeSMS7Bit(PDU : string):string;
function ReverseStr(SourceStr : string) : string;
function HexCharToInt(HexToken : char):Integer;
function HexCharToBin(HexToken : char): string;
function pow(base, power: integer): integer;
function BinStrToInt(BinStr : string) : integer;
function DecodeSMS7Bit(PDU : string):string;
function ReverseStr(SourceStr : string) : string;
implementation
uses sysutils, dialogs;
function HexCharToInt(HexToken : char):Integer;
begin
{if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }
begin
{if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }
Result:=0;
if (HexToken>#47) and (HexToken<#58) then { chars 0....9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { chars A....F }
Result:=Ord(HexToken)-65 + 10;
end;
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { chars A....F }
Result:=Ord(HexToken)-65 + 10;
end;
function HexCharToBin(HexToken : char): string;
var DivLeft : integer;
begin
DivLeft:=HexCharToInt(HexToken); { first HEX->BIN }
Result:='';
{ Use reverse dividing }
repeat { Trick; divide by 2 }
if odd(DivLeft) then { result = odd ? then bit = 1 }
Result:='1'+Result { result = even ? then bit = 0 }
else
Result:='0'+Result;
var DivLeft : integer;
begin
DivLeft:=HexCharToInt(HexToken); { first HEX->BIN }
Result:='';
{ Use reverse dividing }
repeat { Trick; divide by 2 }
if odd(DivLeft) then { result = odd ? then bit = 1 }
Result:='1'+Result { result = even ? then bit = 0 }
else
Result:='0'+Result;
DivLeft:=DivLeft div 2; { keep dividing till 0 left and length = 4 }
until (DivLeft=0) and (length(Result)=4); { 1 token = nibble = 4 bits }
end;
until (DivLeft=0) and (length(Result)=4); { 1 token = nibble = 4 bits }
end;
function HexToBin(HexNr : string): string;
{ only stringsize is limit of binnr }
var Counter : integer;
begin
Result:='';
{ only stringsize is limit of binnr }
var Counter : integer;
begin
Result:='';
for Counter:=1 to length(HexNr) do
Result:=Result+HexCharToBin(HexNr[Counter]);
end;
Result:=Result+HexCharToBin(HexNr[Counter]);
end;
function pow(base, power: integer): integer;
var counter : integer;
begin
Result:=1;
var counter : integer;
begin
Result:=1;
for counter:=1 to power do
Result:=Result*base;
end;
Result:=Result*base;
end;
function BinStrToInt(BinStr : string) : integer;
var counter : integer;
begin
if length(BinStr)>16 then
raise ERangeError.Create(#13+BinStr+#13+
'is not within the valid range of a 16 bit binary.'+#13);
var counter : integer;
begin
if length(BinStr)>16 then
raise ERangeError.Create(#13+BinStr+#13+
'is not within the valid range of a 16 bit binary.'+#13);
Result:=0;
for counter:=1 to length(BinStr) do
if BinStr[Counter]='1' then
Result:=Result+pow(2,length(BinStr)-counter);
end;
if BinStr[Counter]='1' then
Result:=Result+pow(2,length(BinStr)-counter);
end;
function DecodeSMS7Bit(PDU : string):string;
var OctetStr : string;
OctetBin : string;
Charbin : string;
PrevOctet: string;
Counter : integer;
Counter2 : integer;
begin
PrevOctet:='';
Result:='';
var OctetStr : string;
OctetBin : string;
Charbin : string;
PrevOctet: string;
Counter : integer;
Counter2 : integer;
begin
PrevOctet:='';
Result:='';
for Counter:=1 to length(PDU) do
begin
if length(PrevOctet)>=7 then { if 7 Bit overflow on previous }
begin
if BinStrToInt(PrevOctet)<>0 then
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+' ';
begin
if length(PrevOctet)>=7 then { if 7 Bit overflow on previous }
begin
if BinStrToInt(PrevOctet)<>0 then
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+' ';
PrevOctet:='';
end;
end;
if Odd(Counter) then { only take two nibbles at a time }
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);
Charbin:='';
for Counter2:=1 to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];
for Counter2:=1 to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];
for Counter2:=1 to 7-length(PrevOctet) do
Charbin:=OctetBin[8-Counter2+1]+Charbin;
Charbin:=OctetBin[8-Counter2+1]+Charbin;
if BinStrToInt(Charbin)<>0 then Result:=Result+Chr(BinStrToInt(CharBin))
else Result:=Result+' ';
else Result:=Result+' ';
PrevOctet:=Copy(OctetBin,1,length(PrevOctet)+1);
end;
end;
end;
end;
end;
end;
function ReverseStr(SourceStr : string) : string;
var Counter : integer;
begin
Result:='';
var Counter : integer;
begin
Result:='';
for Counter:=1 to length(SourceStr) do
Result:=SourceStr[Counter]+Result;
end;
Result:=SourceStr[Counter]+Result;
end;
end.
在Delphi中没有前导0输出的函数,如C语言中printf("%08d",10);就会输"000000010",但是在实际应用中经常会用到此种函数,如财务等数据的输出。如何实现?以下的代码实现了这种功能,
在Delphi中没有前导0输出的函数,如C语言中printf("%08d",10);就会输"000000010",但是在实际应用中经常会用到此种函数,如财务等数据的输出。如何实现?以下的代码实现了这种功能,
function qd0str(const count,num:integer):String;
Var
s1,s2:String;
begin
s1:=IntToStr(Num);
s2:='00000000000000000000';
if (Length(s1)>=count) then
s2:=''
else if(count>20) then
SetLength(S2,20-Length(s1))
else
SetLength(S2,count-Length(s1));
Var
s1,s2:String;
begin
s1:=IntToStr(Num);
s2:='00000000000000000000';
if (Length(s1)>=count) then
s2:=''
else if(count>20) then
SetLength(S2,20-Length(s1))
else
SetLength(S2,count-Length(s1));
Result:=S2+S1;
end;
end;
函数的参数count为输出字符串的总长度,在函数中定义为最大20。Num为需要转换成字符串的数据。
////////////////////////////////////////
function IntToLZStr(value : integer;digits : byte) : string;
var
Res : string;
////////////////////////////////////////
function IntToLZStr(value : integer;digits : byte) : string;
var
Res : string;
begin
Res:=IntToStr(value);
while Length(Res)<digits do
Insert('0',res,1);
IntToLZStr:=Res;
end;