赞
踩
1、提取字符串中子字符串
- function RightStr
- (Const Str: String; Size: Word): String;
- begin
- if Size > Length(Str) then Size := Length(Str) ;
- RightStr := Copy(Str, Length(Str)-Size+1, Size)
- end;
- // --------------------------------------
- function MidStr
- (Const Str: String; From, Size: Word): String;
- begin
- MidStr := Copy(Str, From, Size)
- end;
- // --------------------------------------
- function LeftStr
- (Const Str: String; Size: Word): String;
- begin
- LeftStr := Copy(Str, 1, Size)
- end;
-
- // --------------------------------------------------
-
- function BetweenString(SrcStr: string; Str1, Str2: string): string;
- var
- Pos1, Pos2: Integer;
- TempStr: string;
- begin
- Pos1 := Pos(Str1, SrcStr);
- TempStr := Copy(SrcStr, Pos1 + Length(Str1), Length(SrcStr) - Length(Str1));
- Pos2 := Pos(Str2, TempStr);
- Result := Copy(TempStr, 1, Pos2 - 1);
- end;
-------------------------------------------------------------
if not TryStrToFloat(Trim(self.CbbCostMultiple.Text), fCostMultiple)then
begin
ShowInfoMsg('计费倍率填写有误!');
exit;
end;
TryStrToFloat:判断转换类型同时进行变量赋值
------------------------------------------------------------------
Delphi 时间的比较
时间比较函数
CompareDateTime() //比较两个时间是否相同(只比较日期部分,不比较年份部份,比较两个日期时间直接可以直接比较A日期<B日期)
CompareDate() //比较两个时间的日期是否相同
CompareTime() //比较两个时间的时分秒是否相同
SameDateTime() //比较两个时间是否相同
SameDate() //比较两个时间的日期部分是否相同
SameTime() //比较两个时间的时分秒是否相同
前3个函数 的返回值是-1,0,1
前者 < 后者 返回 -1
前者 = 后者 返回 0
前者 > 后者 返回 1
后3个函数的返回值是True,False
- program Project1;
-
- {$APPTYPE CONSOLE}
-
- uses
- SysUtils,DateUtils;
-
- var
- date1,date2:TDateTime;
- r1,r2,r3:Integer;
- b1,b2,b3:Boolean;
- begin
- date1:= StrToDateTime('2019-08-08 10:01:00');
- date2:= StrToDateTime('2019-08-08 10:00:00');
-
- r1:=compareDatetime(date1,date2);//1
- r2:=CompareDate(date1,date2); //0
- r3:=CompareTime(date1,date2); //1
-
- b1:=SameDateTime(date1,date2);//FALSE
- b2:=SameDate(date1,date2);//TRUE
- b3:=SameTime(date1,date2);//FALSE
-
- date1:= StrToDateTime('2019-08-08 10:01:00');
- date2:= StrToDateTime('2019-08-08 10:02:00');
-
- r1:=compareDatetime(date1,date2);//-1
- r2:=CompareDate(date1,date2); //0
- r3:=CompareTime(date1,date2); //-1
-
- b1:=SameDateTime(date1,date2);//FALSE
- b2:=SameDate(date1,date2);//TRUE
- b3:=SameTime(date1,date2);//FALSE
-
- date1:= StrToDateTime('2019-09-08 10:01:00');
- date2:= StrToDateTime('2019-08-08 10:01:00');
-
- r1:=compareDatetime(date1,date2);//1
- r2:=CompareDate(date1,date2); //1
- r3:=CompareTime(date1,date2); //0
-
- b1:=SameDateTime(date1,date2);//FALSE
- b2:=SameDate(date1,date2);//FALSE
- b3:=SameTime(date1,date2);//TRUE
-
- end.
-----------------------------------------------------------------------------------
- procedure TForm1.FormCreate(Sender: TObject);
- var
- f : double;
- begin
- f := 1.50;
- showmessage(IntToStr(Trunc(f))); // 1 截取
- showmessage(IntToStr(Round(f))); // 2 四舍五入
- showmessage(IntToStr(Ceil(f))); // 2 向上取整
- showmessage(IntToStr(floor(f))); // 1 向下取整
-
-
- f := 1.40;
- showmessage(IntToStr(Trunc(f))); // 1 截取
- showmessage(IntToStr(Round(f))); // 1 四舍五入
- showmessage(IntToStr(Ceil(f))); // 2 向上取整
- showmessage(IntToStr(floor(f))); // 1 向下取整
-
-
- f := -1.50;
- showmessage(IntToStr(Trunc(f))); // -1 截取
- showmessage(IntToStr(Round(f))); // -2 四舍五入
- showmessage(IntToStr(Ceil(f))); // -1 向上取整
- showmessage(IntToStr(floor(f))); // -2 向下取整
-
-
- f := -1.40;
- showmessage(IntToStr(Trunc(f))); // -1 截取
- showmessage(IntToStr(Round(f))); // -1 四舍五入
- showmessage(IntToStr(Ceil(f))); // -1 向上取整
- showmessage(IntToStr(floor(f))); // -2 向下取整
-
-
- // showmessage(IntToStr(Integer(f))); //不能这样 Error
-
-
- // 截取就是你能看到的整数是多少就是多少
- // 四舍五入, 这个不用多说了
- // 向上取整, 比这个值大的 最小整数
- // 向下取整, 比这个值小的 最大整数
- end;
=========================================================
string字符串转换Byte[]字节数组
- procedure TForm1.Button1Click(Sender: TObject);
-
- var
-
- s:string;
-
- P:PChar;
-
- B:array of Byte;
-
- begin
-
- s:='Hello';
-
- SetLength(B,Length(S)+1);
-
- P:=PChar(S);
-
- CopyMemory(B,p,Length(S)+1);
-
- Showmessage(Char(B[0]));
-
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
-
- var
-
- s:string;
-
- ab:array of byte;
-
- i:integer;
-
- begin
-
- s:='this is a test';
-
- SetLength(ab,Length(s));
-
- for i:=1 to length(s) do
-
- ab[i]:=byte(s[i]);
-
- end;
-
- b: array[1..7] of byte;
-
-
-
- s: string;
-
-
-
-
-
- SetLength(s, 7);
-
-
-
- Move(b[1],s[1], 7 );
Delphi Val函数
在这里Val和iif都是你所用的数据库中的函数
在delphi中Val是一个将字符串转换为数字的函数,
Val(S; var V; var Code: Integer)第一个参数是要转换的字符串,第二个参数存放转换后的数字,可以是整数或浮点数,第三个参数存放出错的字符索引值,例如:
Var
V, Code: Integer;
begin
Val('123.445',V,Code);
end;
转换后: V = 123, Code = 4,表示字符串'123.445'转换为整数是123,在字符串第4位转换出错
=============================================
=====================================================
搜索文件返回搜索结果
- procedure TGlobal.MakeFileList(Path, FileExt: string;out LFileList :TStringList);
- var
- sch: TSearchrec;
- begin
- {确认路径为文件夹}
- if(rightStr(trim(Path),1)<>'\')then
- Path := trim(Path) + '\'
- else
- Path := trim(Path);
-
-
- {确认路径正确}
- if not DirectoryExists(Path) then exit;
-
-
- if FindFirst(Path + '*', faAnyfile, sch) = 0 then
- begin
- repeat
- Application.ProcessMessages;
- if ((sch.Name = '.') or (sch.Name = '..')) then
- Continue;
-
- if DirectoryExists(Path + sch.Name) then
- begin
- MakeFileList(Path + sch.Name, FileExt, LFileList);
- // LFileList.AddStrings(LFileList); // ccm 20220901改,解决Out of Memory
- end
- else
- begin
- // if(UpperCase(extractfileext(Path + sch.Name)) = UpperCase(FileExt)) or (FileExt = '.*') then
- if(UpperCase(extractfileext(Path + sch.Name)) = UpperCase(FileExt)) then // ccm 20220901改,解决Out of Memory
- LFileList.Add(Path + sch.Name);
- end;
- until FindNext(sch) <> 0;
- SysUtils.FindClose(sch);
- end;
-
- end;
================================
获取程序的名称
ChangeFileExt(ExtractFileName(Application.ExeName), '')
================================
PAnsiChar转String
- var
- aRecvByteArr: array of Byte;
- MyStream :TMemoryStream;
- pCommType :TCommType;//读命令为优先级显示至主窗口
- rstr :String;
- tpDevice :pEnMeter;
- strIDNum :string;
- temprstr :string;
- begin
- rstr := '';
- SetLength(aRecvByteArr, pvReqeust.RecvBufferLen);
- Move(pvReqeust.RecvBuffer^, aRecvByteArr[0], pvReqeust.RecvBufferLen);
- SetLength(rstr, pvReqeust.RecvBufferLen);
- CopyMemory(PAnsiChar(rstr), @aRecvByteArr[0], pvReqeust.RecvBufferLen);
- rstr := StrToHexStr(rstr);
============================================================
字符串转16进制字符串
function StrToHexStr(const S:string):string;
//字符串转换成16进制字符串
var
I :integer;
begin
result:='';
for I:=1 to Length(S) do result:=result+IntToHex(Ord(S[I]),2);
end;
==================================================================
字节转AnsiString
var
aRecvByteArr:array of Byte;
s:AnsiString;
i:Integer;
begin
SetLength(aRecvByteArr,pvReqeust.RecvBufferLen);
Move(pvReqeust.RecvBuffer,aRecvByteArr[0],pvReqeust.RecvBufferLen);
for i := 0 to pvReqeust.RecvBufferLen do
begin
s := s+ IntToHex(aRecvByteArr[i],2);
end;
===================================================
金额大写
- function Tform1.SmallTOBig(small:real):string;
- var SmallMonth,BigMonth:string;
- wei1,qianwei1:string[2];
- wei,qianwei,dianweizhi,qian:integer;
- IsNeg: Boolean;
- begin
- {------- 修改参数令值更精确 -------}
- {小数点后的位数,需要的话也可以改动该值}
- qianwei:=-2;
-
- {转换成货币形式,需要的话小数点后加多几个零}
- Smallmonth:=formatfloat('0.00',small);
- {---------------------------------}
- // 处理负数,正数不用这段代码
- if Pos('-',Smallmonth) > 0 then
- begin
- IsNeg := True;
- Smallmonth := StringReplace(Smallmonth,'-','',[]);
- end;
- dianweizhi :=pos('.',Smallmonth);{小数点的位置}
-
- {循环小写货币的每一位,从小写的右边位置到左边}
- for qian:=length(Smallmonth) downto 1 do
- begin
- {如果读到的不是小数点就继续}
- if qian<>dianweizhi then
- begin
-
- {位置上的数转换成大写}
- case strtoint(copy(Smallmonth,qian,1)) of
-
- 1:wei1:='壹'; 2:wei1:='贰';
- 3:wei1:='叁'; 4:wei1:='肆';
- 5:wei1:='伍'; 6:wei1:='陆';
- 7:wei1:='柒'; 8:wei1:='捌';
- 9:wei1:='玖'; 0:wei1:='零';
- end;
-
- {判断大写位置,可以继续增大到real类型的最大值,可是谁有那么多钱}
- case qianwei of
- -3:qianwei1:='厘';
- -2:qianwei1:='分';
- -1:qianwei1:='角';
- 0 :qianwei1:='元';
- 1 :qianwei1:='拾';
- 2 :qianwei1:='佰';
- 3 :qianwei1:='千';
- 4 :qianwei1:='万';
- 5 :qianwei1:='拾';
- 6 :qianwei1:='佰';
- 7 :qianwei1:='千';
- 8 :qianwei1:='亿';
- 9 :qianwei1:='十';
- 10:qianwei1:='佰';
- 11:qianwei1:='千';
- end;
-
- inc(qianwei);
- BigMonth :=wei1+qianwei1+BigMonth;{组合成大写金额}
- end;
-
- end;
- BigMonth := StringReplace(BigMonth, '零仟', '零',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零佰', '零',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零拾', '零',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零角', '零',[rfReplaceAll]);
-
- BigMonth := StringReplace(BigMonth, '零零', '零',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零零', '零',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零零', '零',[rfReplaceAll]);
-
- BigMonth := StringReplace(BigMonth, '零亿', '亿',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零万', '万',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零分', '整',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零元', '元',[rfReplaceAll]);
-
- if BigMonth = '整' then
- Result := '零元整'
- else
- begin
- BigMonth := StringReplace(BigMonth, '亿万', '亿零',[rfReplaceAll]);
- BigMonth := StringReplace(BigMonth, '零元', '元',[rfReplaceAll]);
- end;
-
- SmallTOBig:=BigMonth;
- if IsNeg then
- SmallTOBig:='负' + BigMonth;
-
- end;
- function MoneySpeechC(Money: Currency): string; //数字转换为中文大写
- var
- temp: string;
- resu: string;
- i, j: integer;
- len: integer;
- Num: array[0..9] of string;
- A: array[0..13] of string;
- begin
- Num[0] := '零';
- Num[1] := '壹';
- Num[2] := '贰';
- Num[3] := '叁';
- Num[4] := '肆';
- Num[5] := '伍';
- Num[6] := '陆';
- Num[7] := '柒';
- Num[8] := '捌';
- Num[9] := '玖';
- A[0] := '分';
- A[1] := '角';
- A[2] := '元';
- A[3] := '拾';
- A[4] := '佰';
- A[5] := '仟';
- A[6] := '万';
- A[7] := '拾';
- A[8] := '佰';
- A[9] := '仟';
- A[10] := '亿';
- A[11] := '拾';
- A[12] := '佰';
- A[13] := '仟';
- temp := trim(inttostr(round(Money * 100)));
- len := length(temp);
- resu := '';
- if (len > 13) or (len = 0) then
- begin
- exit;
- end;
- for i := 1 to len do
- begin
- j := strtoint(copy(temp, i, 1));
- resu := resu + Num[j] + A[len - i];
- end;
- len := length(resu);
- result := copy(resu, 0, len - 8);
- end;
查找进程
- function FindProcess(AFileName: string): boolean;
- var
- hSnapshot: THandle; //用于获得进程列表
- lppe: TProcessEntry32; //用于查找进程
- Found: Boolean; //用于判断进程遍历是否完成
- begin
- Result := False;
- hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //获得系统进程列表
- lppe.dwSize := SizeOf(TProcessEntry32); //在调用Process32First API之前,需要初始化lppe记录的大小
- Found := Process32First(hSnapshot, lppe); //将进程列表的第一个进程信息读入ppe记录中
- while Found do
- begin
- if ((UpperCase(ExtractFileName(lppe.szExeFile)) = UpperCase(AFileName)) or (UpperCase(lppe.szExeFile) = UpperCase(AFileName))) then
- begin
- Result := True;
- end;
- Found := Process32Next(hSnapshot, lppe); //将进程列表的下一个进程信息读入lppe记录中
- end;
- end;
==============================================
CheckSum函数
- function TForm1.GetBCDStr(tstr: string): string;
- var
- j: integer;
- begin
- result := '';
- j := length(tstr);
- while j > 0 do
- begin
- result := result + copy(tstr, j - 1, 2);
- j := j - 2;
- end;
- end;
-
- function TForm1.GetCheckSum(SourceStr: string): string;
- begin
- SourceStr := copy(SourceStr, 1, length(SourceStr) - 4);
- result := self.GetBCDStr(self.GetCRC16Data(SourceStr));
- result := SourceStr + result;
- end;
-
- function TForm1.GetCRC16Data(SourceStr: string): string;
- const
- GENP = $A001; //多项式公式X16+X15+X2+1(1100 0000 0000 0101)
- var
- Data: array[0..255] of Byte;
- i, j, Count: Integer;
- Res: Word;
- szData: string;
- crc: Word;
- tmp: Byte;
-
- procedure CalOneByte(AByte: Byte);//计算1个字节的校验码
- var
- j: Integer;
- begin
- crc := crc xor AByte; //将数据与CRC寄存器的低8位进行异或
- for j := 0 to 7 do //对每一位进行校验
- begin
- tmp := crc and 1; //取出最低位
- crc := crc shr 1; //寄存器向右移一位
- crc := crc and $7FFF; //将最高位置0
- if tmp = 1 then //检测移出的位,如果为1,那么与多项式异或
- crc := crc xor GENP;
- crc := crc and $FFFF;
- end;
- end;
-
- function CalCRC16(AData: array of Byte; AStart, AEnd: Integer): Word;
- var
- i: Integer;
- begin
- crc := $FFFF; //将余数设定为FFFF
- for i := AStart to AEnd do //对每一个字节进行校验
- CalOneByte(AData[i]);
- Result := crc;
- end;
-
- begin
-
- szData := SourceStr; //读入欲校验的字符串
- Count := length(SourceStr) div 2; //读入需要计算的字符串长度
- if not (Count mod 2) = 0 then
- exit; //确保有效字节
-
- i := 1;
- j := 0;
- for j := 0 to Count - 1 do
- begin
- if (i mod 2) = 0 then //每2个字符放入一个字节中
- i := i + 1;
- if i >= Length(szData) then
- exit;
- Data[j] := StrToInt('$' + copy(szData, i, 2)); //取出字符并转换为16进制数
- i := i + 1;
- end;
-
- Res := CalCRC16(Data, Low(Data), Count - 1);
- result := IntToHex(Res, 4);
- end;
=================================================
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。