Delphi中常⽤字符串处理函数
假设str为'abcdef',copy(str,3,2)='cd',copy(str,4,10)='def'
3.length(str) 返回str的字符个数,即其长度.
4.pos(obj,target) 在target字符串中出第⼀个出现obj的第⼀个字符位置,如果不到,返回0.
5.AnsiStrLastChar('你好')结果是“好”。如果有半个汉字出现,返回这半个汉字。⼆者字符串长度分别为2和1。
6.CompareStr - 区分⼤⼩写
7.CompareText - 不区分⼤⼩写
8.StringReplace(const S, OldPattern, NewPattern: string;Flags: TReplaceFlags): string;
字符串替换函数,需要引⽤SysUtils单元
rfReplaceAll:全部替换
rfIgnoreCase:忽略⼤⼩写
使⽤⽅法Lg:
str:='01231142211:655767';//需要把:替换成----
s:=StringReplace(str,':','----',[rfReplaceAll]);
For Example:
var
aStr: String;
字符串replace函数begin
aStr := 'This is a book, not a pen!';
ShowMessage(StringReplace (aStr, 'a', 'two', [])); //This is two book, not a pen!只替换了第⼀个符合的字
ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll]));
//This is two book, not two pen!替换了所有符合的字
aStr := 'This is a book, not A pen!';
ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll])); //This is two book, not A pen!只替换了符合的字(⼩写a)
ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll, rfIgnoreCase])); //This is two book, not two pen!不管⼤⼩写替换了所有符合的字end;
9.delete是删除⼀个字符串中的某部分字符
⽤法是delete(str,//被删除的字符串
index,//从第⼏个字符开始删除
count //删除⼏个
);
Delete(S, 2, 2); 就是从S中的第⼆个开始删除,删除2个字符即2、3.所以结果是145.
////////////////////////////////////////////////////////
LEFTSTR, MIDSTR, RIGHTSTR的介绍
这⼏个函数都包含在StrUtils中,所以需要uses StrUtils;
假设字符串是 Dstr := ’Delphi is the BEST’, 那么
LeftStr(Dstr, 5) := ’Delph’
MidStr(Dstr, 6, 7) := ’i is th’
RightStr(Dstr, 6) := ’e BEST’
8.{判断字符是否是数字}
function IsDigit(ch: char): boolean;
begin
Result := ch in ['0'..'9'];
end;
9、{判断字符是否是⼤写字符}
function IsUpper(ch: char): boolean;
begin
Result := ch in ['A'..'Z'];
end;
10、{判断字符是否是⼩写字符}
function IsLower(ch: char): boolean;
begin
Result := ch in ['a'..'z'];
end;
11、{转换为⼤写字符}
function ToUpper(ch: char): char;
begin
Result := chr(ord(ch) and $DF);
end;
12、{转换为⼩写字符}
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 := 1to Length(Result) do
begin
if CapitalizeNextLetter and IsLower(Result[i]) then
Result[i] := ToUpper(Result[i]);
CapitalizeNextLetter := Result[i] = '';
end;
end;
////////////////////////////////////////////////////////////
13.{返回两个⼦字符串之间字符的个数}
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;
14.{更快速的字符查询,快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;
/////////////////////////////
15.替换字符串中⼦串的函数,他可以从字符串中出指定⼦串,并替换为另⼀⼦串。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 > 0then 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 > 0then 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 ( string ; Var S:string ) : string ; < /span>
Var
F : Word ;
begin
F := POS (Src,S) ;
if F=0then
Before := S
else
Before := COPY(S,1,F-1) ;
end ;
//////////////////////////////////
提取字符串中指定⼦字符串后的字符串
Function After ( string ; Var S:string ) : string ; < /span>
Var
F : Word ;
begin
F := POS (Src,S) ;
if F=0then
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)>0do
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)=0then// Nothin' to separate
Exit;
p:=Pos(Separator,s);
while P<>0do
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)>0then
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:=0to terms.Count-1do
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 := 1to 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:/');
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)
{ ⽬的 : 反转⼀个字符串
{===============================================================} unit BinHexTools;
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;
implementation
uses sysutils, dialogs;
function HexCharToInt(HexToken : char):Integer;
begin
{if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }
Result:=0;
if (HexToken>#47) and (HexToken<#58) then{ 9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then{ 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;
DivLeft:=DivLeft div2; { keep dividing till 0 left and length = 4 }
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:='';
for Counter:=1to length(HexNr) do
Result:=Result+HexCharToBin(HexNr[Counter]);
end;
function pow(base, power: integer): integer;
var counter : integer;
begin
Result:=1;
for counter:=1to power do
Result:=Result*base;
end;
function BinStrToInt(BinStr : string) : integer;
var counter : integer;
begin
if length(BinStr)>16then
raise ERangeError.Create(#13+BinStr+#13+
'is not within the valid range of a 16 bit binary.'+#13);
Result:=0;
for counter:=1to length(BinStr) do
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:='';
for Counter:=1to length(PDU) do
begin
if length(PrevOctet)>=7then{ if 7 Bit overflow on previous }
begin
if BinStrToInt(PrevOctet)<>0then
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+'';
PrevOctet:='';
end;
if Odd(Counter) then{ only take two nibbles at a time }
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);
Charbin:='';
for Counter2:=1to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];
for Counter2:=1to7-length(PrevOctet) do
Charbin:=OctetBin[8-Counter2+1]+Charbin;
if BinStrToInt(Charbin)<>0then Result:=Result+Chr(BinStrToInt(CharBin))
else Result:=Result+'';
PrevOctet:=Copy(OctetBin,1,length(PrevOctet)+1);
end;
end;
end;
function ReverseStr(SourceStr : string) : string;
var Counter : integer;
begin
Result:='';
for Counter:=1to length(SourceStr) do
Result:=SourceStr[Counter]+Result;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论