?? strprocess.pas
字號:
{
例程作者:李龍武。版權歸洪濤軟件所有 1999-12-1
例程說明:
}
unit StrProcess;
interface
uses
Classes,SysUtils,Graphics,dialogs;
const
TooLong=' is too long!';
EmptyStr=' is an empty string value!';
EmptyHex=' is an empty Hex value!';
EmptyBinary=' is an empty binary value!';
InvalidHex=' is invalid Hex value!';
InvalidSize=' is a invalid value with the binary size.';
InvalidBinary=' is invalid binary value!';
LoopReplace1='The string replaced and the string replacing with are the SAME!';
LoopReplace2='Case Sensitive is FALSE.'+LoopReplace1;
type
EStrToCharError=class(exception);
EHexToIntError=class(exception);
EIntToBinError=class(exception);
EBinToIntError=class(exception);
EReplaceError=class(exception);
TStrException=class of exception;
{ 數制轉換}
function HexToInt(Hex:String):integer; //將一個十六進制的值轉換成整型
function IntToBin(Int: LongInt;Size: Integer): String; //將一個十進制整型轉換成二進制值
function BinToInt(Bin: string): LongInt; //將一個十進制整型轉換成二進制值
function BinToHex(Bin:string;Size:integer):string; //將二進制轉換成十六進制
function HexToBin(Hex:string;Size:integer):string; //將十六進制轉換成二進制
{字符處理}
//字符的種類
function IsNumber(Ch: char):boolean; //判斷一個字符是否是一個數字
function IsLetter(Ch:char):boolean; //判斷一個字符是否是一個字母
function IsSign(Ch:char):boolean;//判斷一個字符是否是一些符號' ','\' '>'等。
function IsPrint(Ch:char):boolean;//判斷一個字符是否是可顯示出來的字符
{字符串處理}
//串轉換/轉化
function StrToChar(Str:string):char; //將單個字符的串轉換成字符
function IntToStrPad0(Digital,Size:integer;Before:boolean):string;//將一個整型數轉換成一個在前/后添加若干個0的串
//字符數目
function GetMaxWordNumber(var MaxLineID:integer;List:TStringList):integer; //獲取最大行的行號及字母數。
function GetChineseWordNumber(Str:string):integer; //獲取一個串中漢字的個數
function GetEnglishWordNumber(Str:string):integer; //獲取一個串中英文字母個數
//字符輸出空間
function GetTextOutWidth(Canvas:TCanvas;Str:String;WordSpace:integer):integer;//獲取一個串輸出時的寬度(Pixel)
function GetTextOutHeight(Canvas:TCanvas;List:TStringList;LineSpace:integer):integer;//獲取StringList輸出時的高度。
//串的排列順序
function Reversed(Str:string):string; //倒排字符串
//子串在串中的位置
function ReversedPos(SubStr,Str:string):integer; //查找最右邊的SubStr在Str中的位置。與Pos相反。
function LeftLastPos(Str:string;FindChar:Char):integer;//查找某一個字符在串左邊的最后的位置
function RightBeforePos(Str:string;FindChar:char):integer;//查早某一字符在右邊最前的位置
function LastPos(Str:string;FindChar:char):integer;overload;//查找某一字符在串中最后的位置。返回0表示沒有這樣的字符
function LastPos(Str:string;FindStr:string):integer;overload;//查找某一字符在串中最后的位置。返回0表示沒有這樣的字符
function AnyPos(Str,FindStr:string;CaseSensitive:boolean):integer;//可以按是否區分大小寫來查找第一個FindStr的位置,其余功能同Pos
function AnyLastPos(Str,FindStr:string;CaseSensitive:boolean):integer;//可以按是否區分大小寫來查找最后一個FindStr的位置,其余功能同LastPos
procedure GetSubInfoInStr(Str,SubStr:string;CaseSensitive:boolean;var InfoList:TStringList);//獲取一個串中所有子串在串中的位置
//字符串的替換
//字符串的替換--用RepStr代替SignChar位置前、后、之間的的串
function ReplaceLeft(Str,RepStr:string;SignChar:char):string;//RepStr替換SignChar前面的所有字符 。如果沒有找到相應的字符就返回原串
function ReplaceMiddle(Str,RepStr:string;SignChar:char):string; //用RepStr替換第一個和最后一個SignChar 之間的所有字符
function ReplaceRight(Str,RepStr:string;SignChar:char):string;//用RepStr替換SignChar后面的所有字符.如果沒有找到相應的字符就返回原串
function ReplaceBoth(Str,RepStr:string;SignChar:char):string;//用RepStr替換左邊第一個前面和右邊第一個后面的所有串
//字符串的替換--用SpecifiedChar代替SignChar位置前、后、之間的SignChar
function ReplaceLeftChar(Str:string;RepChar,SignChar:char):string;//用RepChar替換從左邊到第一個不是SignChar之間的所有SignChar字符
function ReplaceMiddleChar(Str:string;RepChar,SignChar:char):string;//用RepChar替換從左邊到第一個不是SignChar開始 到 從右邊開始到第一個不是SignChar之間的所有SignChar
function ReplaceRightChar(Str:string;RepChar,SignChar:char):string;//用RepChar替換從最右邊到第一個不是SignChar之間的所有SignChar字符
function ReplaceBothChar(Str:string;RepChar,SignChar:char):string;//用RepChar替換從左邊到第一個不是SignChar之間以及從最右邊到第一個不是SignChar之間的所有SignChar字符
function ReplaceAllChar(Str:string;RepChar,SignChar:char):string;//用RepChar代替串中所有的SignChar
//字符串的替換--用RepStr代替SignStr
function ReplaceFirstStr(Str,RepStr,SignStr:string;CaseSensitive:boolean):string;//用RepStr替換第一個SignStr
function ReplaceLastStr(Str,RepStr,SignStr:string;CaseSensitive:boolean):string;//用RepStr替換最后一個SignStr
function ReplaceBothStr(Str,RepStr,SignStr:string;CaseSensitive:boolean):string;//用RepStr替換最前面和最后面一個SignStr
function ReplaceAllStr(Str,RepStr,SignStr:string;CaseSensitive:boolean;var RepCount:integer):string;//用RepStr替換所有的SignStr
//字符串的刪除
function TrimLeftChar(Str:string;DelChar:char):string;//刪除Str中左邊的指定字符
function TrimRightChar(Str:string;DelChar:char):string; //刪除Str中右邊的指定字符
function TrimBoth(Str:string;DelChar:char):string;//刪除Str中左右邊的指定字符
function TrimAll(Str:String;DelChar:char):string;//刪除Str中所有的指定字符
function TrimMiddle(Str:string;DelChar:char):string;//刪除中間的指定字符(從左邊和右邊第一個不是指定字符之間)
//獲取字符串的子串
//字符串獲取-獲取某一字符的左、右、中間的字符串
function GetLeftStr(Str:string;SpecifiedChar:char):string;//獲取字符串某一特定字符(第一個)左邊的所有字符(不含該特定字符)
function GetMiddleStr(Str:string;SpecifiedChar:Char):string;//獲取字符串某一特定字符(第一個和最后一個)之間的所有字符(不含該特定字符)
function GetRightStr(Str:string;SpecifiedChar:char):string;//獲取字符串某一特定字符(第一個)右邊的所有字符(不含該特定字符)
//字符串獲取-獲取某一字符串(該字符串由相同的字符組成)中間的字符串
function GetMiddleString(Str:string;SpecifiedChar:Char):string;
//字符串獲取-獲取兩個字符串相同的部分
function GetLeftSameStr(Str1,Str2:string):string; //獲取兩字符串左邊相同的部分
function GetRightSameStr(Str1,Str2:string):string; //獲取兩字符串右邊相同的部分
//字符串獲取-獲取兩個字符串不同的部分
procedure GetRightDiverse(var Str1,Str2:string); //獲取兩個字符串相同部分的右邊不同部分
procedure GetLeftDiverse(var Str1,Str2:string); //獲取兩個字符串相同部分左邊不同部分
//字符串的比較
function FindDiverseLeftPos(Str1,Str2:string):integer;//從左至右查找在字符串中不同的位置。空格忽略
function FindDiverseRightPos(Str1,Str2:string):integer;//從右至左查找在字符串中不同的位置。 空格忽略
//字符串的插入
function RepeatChar(ReChar:char;Count:integer):string; //將某一字符重復Count次;
implementation
procedure EStrProcess(ExceptType:TStrException;Value,ErrorInfo:string);
begin
raise ExceptType.Create(Value+ErrorInfo);;
end;
////////////////////////////////////////////////////////////////////////////////
{
數制轉換
}
function HexToInt(Hex:String):integer; //將一個十六進制的值轉換成整型 參數說明:Hex:被轉換的十六進制值
var
HexDigital:Set of char;
i:integer;
Digital:string;
begin
HexDigital:=['1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','a','b','c','d','e','f'];
if Length(Hex)=0 then EStrProcess(EHexToIntError,Hex,EmptyHex);
for i:=1 to Length(Hex) do begin
Digital:=Copy(Hex,i,1);
if (i=1)and(digital='$')then Continue;
if not(StrToChar(Digital) in HexDigital) then EStrProcess(EHexToIntError,Hex,InvalidHex);
end;
Digital:=Copy(Hex,1,1);
if Digital<>'$' then Hex:='$'+Hex;
Result:=StrToInt(Hex);
end;
function IntToBin(Int:LongInt;Size:Integer):String; //將一個十進制整型轉換成二進制值 參數說明:Int:被轉換的整型值 Size:轉換后的寬度:4位 8位 或更大
var
i:Integer;
begin
if Size<1 then EStrProcess(EIntToBinError,IntToStr(Size),InvalidSize);
for i:=Size downto 1 do begin
if Int and (1 shl (Size-i))<>0 then Result:='1'+Result
else Result:='0'+Result;
end;
end;
function BinToInt(Bin: string): LongInt;//將一個十進制整型轉換成二進制值 參數說明:Bin:被轉換的二進制值串
var
i,Size: Integer;
Bit:string;
begin
Result:=0;
Size:=Length(Bin);
if Size=0 then EStrProcess(EBinToIntError,Bin,EmptyBinary);
for i:=Size downto 1 do begin
Bit:= Copy(Bin,i,1);
if (Bit<>'1') and (Bit<>'0') then EStrProcess(EBinToIntError,Bin,InvalidBinary);
if Bit='1' then Result:=Result+(1 shl (Size-i));
end;
end;
function BinToHex(Bin:string;Size:integer):string; //將二進制轉換成十六進制 Bin:被轉換的二進制值 Size:轉換成十六進制的寬度
begin
Result:=IntToHex(BinToInt(Bin),Size);
end;
function HexToBin(Hex:string;Size:integer):string; //將十六進制轉換成二進制 Bin:被轉換的十六進制值 Size:轉換成二進制的寬度
begin
Result:=IntToBin(HexToInt(Hex),Size);
end;
/////////////////////////////////////////////////////////////////////////////////
{
字符處理
}
//字符種類
function IsNumber(Ch: char): boolean; //判斷一個字符是否是一個數字
begin
Result:=((Ch>='0')and(Ch<='9'));
end;
function IsLetter(Ch:char):boolean; //判斷一個字符是否是一個字母
begin
Result:=((Ch>='a')and(Ch<='z'))or((Ch>='A')and(Ch<='Z'));
end;
function IsSign(Ch:char):boolean;//判斷一個字符是否是一些符號' ','\' '>'等。
type
TSign=set of Char;
var
Sign:TSign;
begin
Sign:=[' ','~','`','!','@','#','$','%','^','&','*','(',')','-','_','=','+','|','\','[',']','{','}',':',';','''','"','<','>',',','.','?','/'];
Result:=Ch in Sign;
end;
function IsPrint(Ch:char):boolean;//判斷一個字符是否是可顯示出來的字符
begin
Result:=IsLetter(Ch) or IsNumber(Ch) or IsSign(Ch);
end;
//////////////////////////////////////////////////////////////////////////////////
{
字符串處理
}
//串轉換/轉化
function StrToChar(Str:string):Char; //將單個字符的串轉換成字符 參數說明:Str:被轉換的串
begin
if Length(Str)=0 then EStrProcess(EStrToCharError,Str,EmptyStr);
if Length(Str)>1 then EStrProcess(EStrToCharError,Str,Toolong);
Result:=Str[1];
end;
function IntToStrPad0(Digital,Size:integer;Before:boolean):string;//將一個整型數轉換成一個在前/后添加若干個0的串
var
Len:integer;
begin
Result:=IntToStr(Digital);
Len:=Length(Result);
while Len<Size do begin
if Before then Result:='0'+Result
else Result:=Result+'0';
Len:=Length(Result);
end;
end;
//字符數目
function GetMaxWordNumber(var MaxLineID:integer;List:TStringList):integer; //獲取最大行的行號及字母數。
var
i:integer;
Len:integer;
begin
Result:=0;
if List.Count=0 then Exit;
for i:=0 to List.Count-1 do begin
Len:=Length(List.strings[i]);
if Len>=Result then begin
Result:=Len;
MaxLineID:=i;
end;
end;
end;
function GetChineseWordNumber(Str:string):integer; //獲取一個串的漢字的個數
var
i:integer;
CurrentChar:char;
CurrentStr:string;
begin
Result:=0;
if Length(Str)=0 then Exit;
for i:=1 to Length(Str) do begin
CurrentStr:=Copy(Str,i,1);
CurrentChar:=CurrentStr[1];
if Ord(CurrentChar)>=127 then Result:=Result+1;
end;
Result:=Result div 2;
end;
function GetEnglishWordNumber(Str:string):integer; //獲取英文字母的個數
begin
Result:=Length(Str)-2*GetChineseWordNumber(Str);
end;
//字符輸出空間 (圖形模式)
function GetTextOutWidth(Canvas:TCanvas;Str:String;WordSpace:integer):integer; //獲取一個串輸出時的寬度(Pixel)
var
i:integer;
CurrentStr:string;
CurrentChar:Char;
begin
i:=1;
Result:=0;
if Length(Str)=0 then Exit;
while i<=Length(Str) do begin
CurrentStr:=Copy(Str,i,1);
CurrentChar:=CurrentStr[1];
if Ord(CurrentChar)>=127 then begin
CurrentStr:=Copy(Str,i,2);
inc(i);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -