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); {source的长度}
StrLen:=length(source); {删除source字符串}
delete(s,site,StrLen);
{插入target字符串到S中} 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;
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) End else Result := S; end;
////////////////////////
替换全部子字符串的函数
function ReplaceSub(str, sub1, sub2: String): 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;
/////////////////////////
在字符串左右填充指定数量的指定字符
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 ; 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; var
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('
根据某个字符分割字符串的函数
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;
========== = 用 法 ========== var
Terms : TStringList; i : integer; const
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;
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; begin
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)