admin管理员组

文章数量:1530254

2024年7月23日发(作者:)

[delphi函数] 一个遍历所有文件夹的函数2010-03-12 15:13

遍历一个文件夹中的所有文件,有时候非常的有用,比如结合excel操作,就可

以完成对报表的数据的合成和整理。

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,

Forms,

Dialogs,StrUtils, StdCtrls;

type

TForm1 = class(TForm)

lst1: TListBox;

btn1: TButton;

procedure btn1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{--------------------------------------------------------------------

-----------

过程名: MakeFileList 遍历文件夹及子文件夹

作者: SWGWEB

日期: 2007.11.25

参数: Path,FileExt:string 1.需要遍历的目录 2.要遍历的文件扩展

返回值: TStringList

Eg::= MakeFileList( 'E:极品飞车','.exe') ;

:= MakeFileList( 'E:极品飞车','.*') ;

---------------------------------------------------------------------

----------}

function MakeFileList(Path,FileExt:string):TStringList ;

var

sch:TSearchrec;

begin

Result:=;

if rightStr(trim(Path), 1) <> '' then

Path := trim(Path) + ''

else

Path := trim(Path);

if not DirectoryExists(Path) then

begin

;

exit;

end;

if FindFirst(Path + '*', faAnyfile, sch) = 0 then

begin

repeat

sMessages;

if (( = '.') or ( = '..')) then Continue;

if DirectoryExists(Path+) then // 这个地方加上一个判

断,可以区别子文件夹河当前文件夹的操作

begin

ings(MakeFileList(Path+,FileExt));

end

else

begin

if (UpperCase(extractfileext(Path+)) =

UpperCase(FileExt)) or (FileExt='.*') then

(Path+);

end;

until FindNext(sch) <> 0;

ose(sch);

end;

end;

{$R *.dfm}

procedure 1Click(Sender: TObject);

begin

n:='loading';

:= MakeFileList('D:Program Files','.exe');

n:='ok';

end;

end.

[Delphi函数] URL编码的转换问题即%百分号网址问题

我们常用到的搜索引擎,比如百度、谷歌。

我们举个例子,比如搜“我爱你”:

百度:/s?wd=%CE%D2%B0%AE%C4%E3

谷歌:

/search?hl=zh-CN&source=hp&q=%E6%88%91%E7%88%B1%E

4%BD%A0&aq=f&oq=

红色字体其实表示的都是“我爱你”,但是百度的编码是gb2312、谷歌的编码

是utf-8,所以URL转换出来的编码不一样。

在查资料的过程中发现一个url转换很实用的一个网址:共享之

/code/url/?key=%CE%D2%B0%AE%C4%E3

那么我们在delphi网络编程时有时后用到URL编码转换,很多时候我们使用的

是ascII编码和16进制来结合完成,这也不错,今天介绍一种我试验成功的:

源码如下:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,

Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

edt1: TEdit;

btn1: TButton;

mmo1: TMemo;

procedure btn1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

uses HTTPApp;

procedure 1Click(Sender: TObject);

var

s,ss:string;

begin

if Boolean(Length(Trim())) then

begin

s:=Trim();

ss:=HTTPEncode(s);

('gb2312编码为:');

(ss);

ss:=AnsiToUtf8(s);

ss:=HTTPEncode(ss);

('Utf-8编码为:');

(ss);

end;

end;

end.

============

截图:

如果需要解码的话:需要用到UTF8Decode、HttpDecode

javascript有相关的内部函数encodeURI、decodeURI、encodeURIComponent、

decodeURIComponent :

参考相关文档:(鸣谢)

/del/archive/2009/02/26/

/t/20051129/10/#

/xstar2008/blog/item/

/xstar2008/blog/item/

/code/url/?key=%CE%D2%B0%AE%C4%E3

[delphi函数] 字母大小写转换函数 LowerCase UpperCase

//大写

function UpperCase(const S: string): string;

var

Ch: Char;

L: Integer;

Source, Dest: PChar;

begin

L := Length(S);

SetLength(Result, L);

Source := Pointer(S);

Dest := Pointer(Result);

while L <> 0 do

begin

Ch := Source^;

if (Ch >= 'a') and (Ch <= 'z') then Dec(Ch, 32);

Dest^ := Ch;

Inc(Source);

Inc(Dest);

Dec(L);

end;

end;

//小写

function LowerCase(const S: string): string;

var

Ch: Char;

L: Integer;

Source, Dest: PChar;

begin

L := Length(S);

SetLength(Result, L);

Source := Pointer(S);

Dest := Pointer(Result);

while L <> 0 do

begin

Ch := Source^;

if (Ch >= 'A') and (Ch <= 'Z') then Inc(Ch, 32);

Dest^ := Ch;

Inc(Source);

Inc(Dest);

Dec(L);

end;

end;

delphi函数] Delphi判断日期合法的函数

var

A : TdateTime;

begin

if not tryStrToDateTime(,a) then ShowMessage(' 日期非法 ');

end;

[delphi函数] delphi中关于时间差的实例

2009-11-06 15:29

很多时候要用到相差多少天,多少周,多少秒,查了一下资料,整理如下:

首先 uses dateutils;

先自己做了个实例,相关代码如下:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, dateutils;

type

TForm1 = class(TForm)

Button1: TButton;

Memo1: TMemo;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure 1Click(Sender: TObject);

var

sd,nd:Tdatetime;

randid:string;

begin

sd := StrtoDatetime('1981-08-08 00:00:00');

nd := now;

;

('开始测试时间差:');

('起始时间:1981-08-08 00:00:00');

('终止时间:'+datetimetostr(nd));

('年:'+inttostr(YearsBetween(sd,nd)));

('月:'+inttostr(MonthsBetween(sd,nd)));

('周:'+inttostr(WeeksBetween(sd,nd)));

('日:'+inttostr(DaysBetween(sd,nd)));

('时:'+inttostr(HoursBetween(sd,nd)));

('分:'+inttostr(MinutesBetween(sd,nd)));

('秒:'+inttostr(SecondsBetween(sd,nd)));

(#13);

('以秒差为例生成随机数:');

randid :=

inttostr(SecondsBetween(sd,nd))+inttostr(random(9))+inttostr(random(9))+inttostr(ra

ndom(9))+inttostr(random(9));//呵呵,很笨的方法。

(randid);

end;

end.

==================

object Form1: TForm1

Left = 571

Top = 224

BorderIcons = [biSystemMenu, biMinimize]

BorderStyle = bsSingle

Caption = #26102#38388#24046#20989#25968

ClientHeight = 249

ClientWidth = 201

Color = clBtnFace

t = DEFAULT_CHARSET

= clWindowText

= -11

= 'MS Sans Serif'

= []

OldCreateOrder = False

PixelsPerInch = 96

TextHeight = 13

object Button1: TButton

Left = 8

Top = 8

Width = 185

Height = 25

Caption = #28436#31034#26102#38388#24046#20989#25968

TabOrder = 0

OnClick = Button1Click

end

object Memo1: TMemo

Left = 8

Top = 40

Width = 185

Height = 201

TabOrder = 1

end

end

相关函数如下:

{ Range query functions }

function YearsBetween(const ANow, AThen: TDateTime): Integer;

function MonthsBetween(const ANow, AThen: TDateTime): Integer;

function WeeksBetween(const ANow, AThen: TDateTime): Integer;

function DaysBetween(const ANow, AThen: TDateTime): Integer;

function HoursBetween(const ANow, AThen: TDateTime): Int64;

function MinutesBetween(const ANow, AThen: TDateTime): Int64;

function SecondsBetween(const ANow, AThen: TDateTime): Int64;

function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;

{ Range spanning functions }

{ YearSpan and MonthSpan are approximates, not exact but pretty darn close }

function YearSpan(const ANow, AThen: TDateTime): Double;

function MonthSpan(const ANow, AThen: TDateTime): Double;

function WeekSpan(const ANow, AThen: TDateTime): Double;

function DaySpan(const ANow, AThen: TDateTime): Double;

function HourSpan(const ANow, AThen: TDateTime): Double;

function MinuteSpan(const ANow, AThen: TDateTime): Double;

function SecondSpan(const ANow, AThen: TDateTime): Double;

function MilliSecondSpan(const ANow, AThen: TDateTime): Double;

找到一篇关于时间函数应用的学习笔记,备份一下。

TDateTime是一个比较常用的类型,用于表达日期时间类型的数据。但是,刚刚接触delphi的

新手在使用这个类型的时候往往会不知所措,不知道该怎样使用才能得到自己想要的结果。这里

说说我在使用过程中的一点心得,其中大部分的内容是来自于delphi帮助,所以如果看了这篇

文章对TDateTime还有什么不清楚的可以去看看Delphi的帮助。另外在这篇文章里我也会告诉

大家我使用帮助的心得,这对老手或许是班门弄斧但是对于新手我自认为还是很有帮助的。

一、关于TDateTime

1、TDateTime的基本概念:

根据Delphi的帮助里所说的,TDateTime是date和time例程用来存放date和time变量的,

在delphi里TDateTime类型本质上是Double类型的,其中整数位用于表达从1899年12月30

日到现在所已经过去的天数,小数部分用于表示当天已经流逝的分数值——有些不明白吧,举个

例子:上午6点小数部分就是0.25、中午12点小数部分就是0.5依此类推。

2、常用函数和过程

2.1 Date函数

function Date: TDateTime;

返回当前的日期,实际类型是TDateTime,也就是小数部分为0的DateTime值,小数部分为0

代表什么意思呢?根据TDateTime的概念我们知道这代表午夜也就是0点。

2.2 DateToStr

函数的形式:function DateToStr(Date: TDateTime): string;

返回值是个字符串,很有用吧。不过,别高兴的太早,如果你用

DateToStr(),你会发现返回的字符串可能是“04-22-03”,这是为什么

呢?这是因为delphi内部有一些系统变量用来定义数字、货币、日期的格式,这些变量delphi

称为Currency and date/time formatting variables,有23个。我也没有完全看懂,这里讲

几个我搞明白的说说。

var ThousandSeparator: Char;

千分位的符号,一般都是西文的逗号“,”,默认值存放在LOCALE_STHOUSAND变量中(位于

windows单元)

var DecimalSeparator: Char;

小数点的符号,一般都是西文的句号“.”,默认值存放在变量LOCALE_SDECIMAL中

var CurrencyDecimals: Byte;

小数点后面保留的位数,一般是2位,默认值存放在变量LOCALE_ICURRDIGITS中

var DateSeparator: Char;

日期的分隔符号,一般使用“-”或“.”,默认值为“-”,默认值存放在变量LOCATE_SDATE

var ShortDateFormat: string;

短日期格式,一般是“”,默认值存放在变量LOCALE_SSHORTDATE中

var LongDateFormat: string;

长日期格式,一般是“”,默认值存放在变量LOCALE_SLONGDATE中

var TimeSeparator: Char;

时间的分隔符,一般是西文的分号“:”,默认值就是这个,默认值存放在变量 LOCALE_STIME

var TimeAMString: string;

表示上午的字符串,默认为“AM”,默认值存放在变量LOCALE_S1159中

var TimePMString: string;

表示下午的字符串,默认为“PM”,默认值存放在变量LOCALE_S2359中

var ShortTimeFormat: string;

短时间格式,默认值存放在变量LOCALE_ITIME LOCALE_ITLZERO中

var LongTimeFormat: string;

长时间格式,一般为“hh:mm:ss”,默认值存放在变量LOCALE_ITIME and LOCALE_ITLZERO中

所以,如果想开发一个稳健的系统,那么这些系统变量必须要在进入系统的时候进行初始化,下

面是我得代码:

gID:=LANG_CHINESE;

DateSeparator:='.';

LongDateFormat:='';

ShortDateFormat:='';

TimeSeparator:=':';

LongTimeFormat:='hh:nn:ss';

TimeAMString:='上午';

TimePMString:='下午';

大家一定奇怪,为什么我的LongDateFormat和ShortDateFormat是一样的,老实讲,我也希望

能够区分长短日期的格式,但是由于有些关于TDateTime的例程在类型转换的时候要参考

ShortDateFormat,所以只能设置成一样的了。

还是继续说DateToStr函数。其他没什么特别的,需要注意的是此函数是根据ShortDateFormat

来进行转换的,所以ShortDateFormat我只能设为“”

说道了DateTimePicker,注意在它的format属性中设置显示格式的时候月份要用“MM”而不是

我们通常认为的“mm”。

2.3 StrToDate函数

function StrToDate(const S: string): TDateTime;

这个函数可以看成是DateToStr的逆函数,作用就是把一个字符串转换为TDateTime,参数S必

须包括2个或3个数字,用DateSeparator所定义的分隔符分隔,年月日的顺序和格式遵从

ShortDateFormat。如果输入的参数不符合日期类型的规定系统会触发EConvertError例外。

2.4 DateTimeToStr函数

function DateTimeToStr(DateTime: TDateTime): string;

把TDateTime转换为字符串,日期格式遵从ShortDateFormat,时间格式遵从LongTimeFormat,

如果TDateTime的小数部分是0的话,那么返回的字符串中将没有时间部分。

2.5 StrToDateTime函数

function StrToDateTime(const S: string): TDateTime;

这个函数可以看作是DateTimeToStr函数的逆函数,和StrToDate差不多。

2.6 DateTimeToString过程

procedure DateTimeToString(var Result: string; const Format: string; DateTime:

TDateTime);

这个过程的作用和DateTimeToStr和DateToStr一样,特别的地方是可以指定TDateTime的格式,

这样就可以得到我们指定格式的日期型字符串了。

2.7 Now,Time,StrToTime等等就不详细介绍了,都可以在帮助里得到相关信息,如果有不明白

的地方就写代码测试一下。

二、怎样使用帮助

delphi的帮助不如PowerBuilder的帮助那么详细,并且外面的书也都

没有什么很好的详尽讲解

以Delphi6中的DateTimeToStr函数为例子,在代码中我们选中

DateTimeToStr,然后按F1就可以得到如下的帮助:【】里是说明

VCL Reference【相关的vcl refrence帮助】

DateTimeToStr function【一般在帮助主题的索引里看到的就是这个】

See also Example【see also链接着相关的内容,Example链接着范例

代码】

Converts a TDateTime value to a string.【功能说明,把TDateTime

类型转换为String类型】

Unit【所属单元】

SysUtils【说明属于SysUtils】

Category【所属类别】

date/time routines【说明属于date/time例程,并且链接着date/time

相关的例程——函数和过程】

function DateTimeToStr(DateTime: TDateTime): string;【函数声名

的形式】

Description【描述,详细的说明】

DateTimeToString converts the TDateTime value given by DateTime

using the format given by the ShortDateFormat global variable,

followed by the time using the format given by the LongTimeFormat

global variable. The time is not displayed if the fractional part

of the DateTime value is zero. 【DateTimeToStr转换通过DateTime

参数传入的TDateTime类型的值为字符串,根据ShortDateFormat全局

变量所给定的日期格式,遵循LongTimeFormat所给定的时间格式。如

果DateTime的小数部分为零的话返回的字符串中将没有时间部分】

To change how the string is formatted, change ShortDateFormat and

LongTimeFormat global date time formatting variables.【要改变

字符串的格式,可以通过改变ShortDateFormat和LongTimeFormat全

局日期时间格式变量来实现】

三、和数据库中的日期字段

有人询问怎样在SQL中设定Date/DateTime类型的查询值,这和数

据库本身有关。不同的数据库对日期类型的字段一般都有对应的函数和

格式,不过应该都支持ANSI标准——谁叫数据库厂商都是美国的。比

如,我在Oracle8中要查询日期型字段我一般使用【Select * From

LoginTable Where LoginTime >= To_Date('2003.03.01

00:00:00',' hh24:mi:ss')】,MSSQLServer2000中文版的

写法是【Select * From LoginTable Where LoginTime >= '2003-03

-01 00:00:00'】,SQLServer的联机帮助中讲得比较详细,而且用起

来似乎也比较方便,不过Oracle可以自己设定日期格式。

说句题外话,查询的时候,很多人为了方便往往把日期型的字段用

函数转换成字符串,然后和字符串式的日期来进行比较,例如:【Select

* From LoginTable Where To_Char(LoginTime,'

hh24:mi:ss') >= '2003.03.01 00:00:00'】,这样的写法语法上没有

问题,但是性能上存在很大的隐患。因为,数据库执行这样的查询是要

对每一条记录的loginTime字段进行计算然后再比较,如果记录数多,

会很慢。

[delphi函数] delphi中关于时间差的实例

很多时候要用到相差多少天,多少周,多少秒,查了一下资料,整理如下:

首先 uses dateutils;

先自己做了个实例,相关代码如下:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, dateutils;

type

TForm1 = class(TForm)

Button1: TButton;

Memo1: TMemo;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure 1Click(Sender: TObject);

var

sd,nd:Tdatetime;

randid:string;

begin

sd := StrtoDatetime('1981-08-08 00:00:00');

nd := now;

;

('开始测试时间差:');

('起始时间:1981-08-08 00:00:00');

('终止时间:'+datetimetostr(nd));

('年:'+inttostr(YearsBetween(sd,nd)));

('月:'+inttostr(MonthsBetween(sd,nd)));

('周:'+inttostr(WeeksBetween(sd,nd)));

('日:'+inttostr(DaysBetween(sd,nd)));

('时:'+inttostr(HoursBetween(sd,nd)));

('分:'+inttostr(MinutesBetween(sd,nd)));

('秒:'+inttostr(SecondsBetween(sd,nd)));

(#13);

('以秒差为例生成随机数:');

randid :=

inttostr(SecondsBetween(sd,nd))+inttostr(random(9))+inttostr(random(9))+inttostr(ra

ndom(9))+inttostr(random(9));//呵呵,很笨的方法。

(randid);

end;

end.

==================

object Form1: TForm1

Left = 571

Top = 224

BorderIcons = [biSystemMenu, biMinimize]

BorderStyle = bsSingle

Caption = #26102#38388#24046#20989#25968

ClientHeight = 249

ClientWidth = 201

Color = clBtnFace

t = DEFAULT_CHARSET

= clWindowText

= -11

= 'MS Sans Serif'

= []

OldCreateOrder = False

PixelsPerInch = 96

TextHeight = 13

object Button1: TButton

Left = 8

Top = 8

Width = 185

Height = 25

Caption = #28436#31034#26102#38388#24046#20989#25968

TabOrder = 0

OnClick = Button1Click

end

object Memo1: TMemo

Left = 8

Top = 40

Width = 185

Height = 201

TabOrder = 1

end

end

相关函数如下:

{ Range query functions }

function YearsBetween(const ANow, AThen: TDateTime): Integer;

function MonthsBetween(const ANow, AThen: TDateTime): Integer;

function WeeksBetween(const ANow, AThen: TDateTime): Integer;

function DaysBetween(const ANow, AThen: TDateTime): Integer;

function HoursBetween(const ANow, AThen: TDateTime): Int64;

function MinutesBetween(const ANow, AThen: TDateTime): Int64;

function SecondsBetween(const ANow, AThen: TDateTime): Int64;

function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;

{ Range spanning functions }

{ YearSpan and MonthSpan are approximates, not exact but pretty darn close }

function YearSpan(const ANow, AThen: TDateTime): Double;

function MonthSpan(const ANow, AThen: TDateTime): Double;

function WeekSpan(const ANow, AThen: TDateTime): Double;

function DaySpan(const ANow, AThen: TDateTime): Double;

function HourSpan(const ANow, AThen: TDateTime): Double;

function MinuteSpan(const ANow, AThen: TDateTime): Double;

function SecondSpan(const ANow, AThen: TDateTime): Double;

function MilliSecondSpan(const ANow, AThen: TDateTime): Double;

找到一篇关于时间函数应用的学习笔记,备份一下。

TDateTime是一个比较常用的类型,用于表达日期时间类型的数据。但是,刚刚接触delphi的

新手在使用这个类型的时候往往会不知所措,不知道该怎样使用才能得到自己想要的结果。这里

说说我在使用过程中的一点心得,其中大部分的内容是来自于delphi帮助,所以如果看了这篇

文章对TDateTime还有什么不清楚的可以去看看Delphi的帮助。另外在这篇文章里我也会告诉

大家我使用帮助的心得,这对老手或许是班门弄斧但是对于新手我自认为还是很有帮助的。

一、关于TDateTime

1、TDateTime的基本概念:

根据Delphi的帮助里所说的,TDateTime是date和time例程用来存放date和time变量的,

在delphi里TDateTime类型本质上是Double类型的,其中整数位用于表达从1899年12月30

日到现在所已经过去的天数,小数部分用于表示当天已经流逝的分数值——有些不明白吧,举个

例子:上午6点小数部分就是0.25、中午12点小数部分就是0.5依此类推。

2、常用函数和过程

2.1 Date函数

function Date: TDateTime;

返回当前的日期,实际类型是TDateTime,也就是小数部分为0的DateTime值,小数部分为0

代表什么意思呢?根据TDateTime的概念我们知道这代表午夜也就是0点。

2.2 DateToStr

函数的形式:function DateToStr(Date: TDateTime): string;

返回值是个字符串,很有用吧。不过,别高兴的太早,如果你用

DateToStr(),你会发现返回的字符串可能是“04-22-03”,这是为什么

呢?这是因为delphi内部有一些系统变量用来定义数字、货币、日期的格式,这些变量delphi

称为Currency and date/time formatting variables,有23个。我也没有完全看懂,这里讲

几个我搞明白的说说。

var ThousandSeparator: Char;

千分位的符号,一般都是西文的逗号“,”,默认值存放在LOCALE_STHOUSAND变量中(位于

windows单元)

var DecimalSeparator: Char;

小数点的符号,一般都是西文的句号“.”,默认值存放在变量LOCALE_SDECIMAL中

var CurrencyDecimals: Byte;

小数点后面保留的位数,一般是2位,默认值存放在变量LOCALE_ICURRDIGITS中

var DateSeparator: Char;

日期的分隔符号,一般使用“-”或“.”,默认值为“-”,默认值存放在变量LOCATE_SDATE

var ShortDateFormat: string;

短日期格式,一般是“”,默认值存放在变量LOCALE_SSHORTDATE中

var LongDateFormat: string;

长日期格式,一般是“”,默认值存放在变量LOCALE_SLONGDATE中

var TimeSeparator: Char;

时间的分隔符,一般是西文的分号“:”,默认值就是这个,默认值存放在变量 LOCALE_STIME

var TimeAMString: string;

表示上午的字符串,默认为“AM”,默认值存放在变量LOCALE_S1159中

var TimePMString: string;

表示下午的字符串,默认为“PM”,默认值存放在变量LOCALE_S2359中

var ShortTimeFormat: string;

短时间格式,默认值存放在变量LOCALE_ITIME LOCALE_ITLZERO中

var LongTimeFormat: string;

长时间格式,一般为“hh:mm:ss”,默认值存放在变量LOCALE_ITIME and LOCALE_ITLZERO中

所以,如果想开发一个稳健的系统,那么这些系统变量必须要在进入系统的时候进行初始化,下

面是我得代码:

gID:=LANG_CHINESE;

DateSeparator:='.';

LongDateFormat:='';

ShortDateFormat:='';

TimeSeparator:=':';

LongTimeFormat:='hh:nn:ss';

TimeAMString:='上午';

TimePMString:='下午';

大家一定奇怪,为什么我的LongDateFormat和ShortDateFormat是一样的,老实讲,我也希望

能够区分长短日期的格式,但是由于有些关于TDateTime的例程在类型转换的时候要参考

ShortDateFormat,所以只能设置成一样的了。

还是继续说DateToStr函数。其他没什么特别的,需要注意的是此函数是根据ShortDateFormat

来进行转换的,所以ShortDateFormat我只能设为“”

说道了DateTimePicker,注意在它的format属性中设置显示格式的时候月份要用“MM”而不是

我们通常认为的“mm”。

2.3 StrToDate函数

function StrToDate(const S: string): TDateTime;

这个函数可以看成是DateToStr的逆函数,作用就是把一个字符串转换为TDateTime,参数S必

须包括2个或3个数字,用DateSeparator所定义的分隔符分隔,年月日的顺序和格式遵从

ShortDateFormat。如果输入的参数不符合日期类型的规定系统会触发EConvertError例外。

2.4 DateTimeToStr函数

function DateTimeToStr(DateTime: TDateTime): string;

把TDateTime转换为字符串,日期格式遵从ShortDateFormat,时间格式遵从LongTimeFormat,

如果TDateTime的小数部分是0的话,那么返回的字符串中将没有时间部分。

2.5 StrToDateTime函数

function StrToDateTime(const S: string): TDateTime;

这个函数可以看作是DateTimeToStr函数的逆函数,和StrToDate差不多。

2.6 DateTimeToString过程

procedure DateTimeToString(var Result: string; const Format: string; DateTime:

TDateTime);

这个过程的作用和DateTimeToStr和DateToStr一样,特别的地方是可以指定TDateTime的格式,

这样就可以得到我们指定格式的日期型字符串了。

2.7 Now,Time,StrToTime等等就不详细介绍了,都可以在帮助里得到相关信息,如果有不明白

的地方就写代码测试一下。

二、怎样使用帮助

delphi的帮助不如PowerBuilder的帮助那么详细,并且外面的书也都

没有什么很好的详尽讲解

以Delphi6中的DateTimeToStr函数为例子,在代码中我们选中

DateTimeToStr,然后按F1就可以得到如下的帮助:【】里是说明

VCL Reference【相关的vcl refrence帮助】

DateTimeToStr function【一般在帮助主题的索引里看到的就是这个】

See also Example【see also链接着相关的内容,Example链接着范例

代码】

Converts a TDateTime value to a string.【功能说明,把TDateTime

类型转换为String类型】

Unit【所属单元】

SysUtils【说明属于SysUtils】

Category【所属类别】

date/time routines【说明属于date/time例程,并且链接着date/time

相关的例程——函数和过程】

function DateTimeToStr(DateTime: TDateTime): string;【函数声名

的形式】

Description【描述,详细的说明】

DateTimeToString converts the TDateTime value given by DateTime

using the format given by the ShortDateFormat global variable,

followed by the time using the format given by the LongTimeFormat

global variable. The time is not displayed if the fractional part

of the DateTime value is zero. 【DateTimeToStr转换通过DateTime

参数传入的TDateTime类型的值为字符串,根据ShortDateFormat全局

变量所给定的日期格式,遵循LongTimeFormat所给定的时间格式。如

果DateTime的小数部分为零的话返回的字符串中将没有时间部分】

To change how the string is formatted, change ShortDateFormat and

LongTimeFormat global date time formatting variables.【要改变

字符串的格式,可以通过改变ShortDateFormat和LongTimeFormat全

局日期时间格式变量来实现】

三、和数据库中的日期字段

有人询问怎样在SQL中设定Date/DateTime类型的查询值,这和数

据库本身有关。不同的数据库对日期类型的字段一般都有对应的函数和

格式,不过应该都支持ANSI标准——谁叫数据库厂商都是美国的。比

如,我在Oracle8中要查询日期型字段我一般使用【Select * From

LoginTable Where LoginTime >= To_Date('2003.03.01

00:00:00',' hh24:mi:ss')】,MSSQLServer2000中文版的

写法是【Select * From LoginTable Where LoginTime >= '2003-03

-01 00:00:00'】,SQLServer的联机帮助中讲得比较详细,而且用起

来似乎也比较方便,不过Oracle可以自己设定日期格式。

说句题外话,查询的时候,很多人为了方便往往把日期型的字段用

函数转换成字符串,然后和字符串式的日期来进行比较,例如:【Select

* From LoginTable Where To_Char(LoginTime,'

hh24:mi:ss') >= '2003.03.01 00:00:00'】,这样的写法语法上没有

问题,但是性能上存在很大的隐患。因为,数据库执行这样的查询是要

对每一条记录的loginTime字段进行计算然后再比较,如果记录数多,

会很慢。

[delphi函数] 字符重复函数

StringOfChar 是反复 "字符" 成 "字符串" 的函数;

DupeString 是反复 "字符串" 成 "新字符串" 的函数;

StringOfChar 来自 System 单元, 可以直接使用;

DupeString 来自 StrUtils 单元, 使用时需要 uses StrUtils;

如果仅仅是反复 "字符", 当然应该用 StringOfChar , 它是用汇编代码实现的, 速度会稍好

一些.

下面是测试代码:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,

Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

procedure FormCreate(Sender: TObject);

procedure Button1Click(Sender: TObject);

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

uses StrUtils; {DupeString 来自这个单元}

procedure eate(Sender: TObject);

var

str: string;

begin

str := OfChar('A', 5);

ShowMessage(str); {AAAAA}

str := ring('A', 5);

ShowMessage(str); {AAAAA}

str := ring('ABC', 5);

ShowMessage(str); {ABCABCABCABCABC}

end;

{如果只是反复字符, StringOfChar 肯定会更快一些; 速度测试:}

procedure 1Click(Sender: TObject);

var

t1,t2: Cardinal;

i: Integer;

begin

t1 := GetTickCount;

for i := 0 to 1000000 do DupeString('A', 5);

t1 := GetTickCount - t1;

t2 := GetTickCount;

for i := 0 to 1000000 do StringOfChar('A', 5);

t2 := GetTickCount - t2;

ShowMessageFmt('DupeString: %d; StringOfChar: %d', [t1,t2]);

end;

end.

[delphi函数] delphi utf8乱码问题

本程序和网上的说法不同,在D7下执行通过

先看一个例子

procedure 1Click(Sender: TObject);

var Response2: TStringStream;

S : string;//注意必须是string ,网上说的widestring 执行空白

begin

Redirects:=true;

Response2 := ('');

();

tType := 'application/x-www-form-urlencoded';

(,Response2);

S:=ring;

S := Utf8ToAnsi(S);

:= S;

;

end;

其中的 utf8 是一种常用的 unicode 编码方式,在目前的网络环境中应用最广,

特别是在java,xml这种国际化很强的地方.delphi并不支持原生的utf8字符串,

它对 unicode 的支持则是非常好了.将普通字符串赋值给 String 类型即可.而

对 uft8 则要用到上面所说的Utf8ToAnsi 等转换函数.好在这些函数都很容易

使用,只是大家在使用时要注意自己当前操作的字符串是什么类型就行了.类似

的函数还有

AnsiToUtf8

UTF8Decode

Utf8ToAnsi

UTF8Decode

UTF8Encode

[delphi函数] Delphi中的split函数

procedure 1Click(Sender: TObject);

var

s1:TStringList;

begin

s1:=split('11111111:2222:33333:44444',':');

ings(s1);

;

end;function split(s,s1:string):TStringList;

begin

Result:=;

while Pos(s1,s)>0 do

begin

(Copy(s,1,Pos(s1,s)-1));

Delete(s,1,Pos(s1,s));

end;

(s);

end;

procedure 1Click(Sender: TObject);

var

s1:TStringList;

begin

s1:=split('11111111:2222:33333:44444',':');

ings(s1);

;

end;

一、直接使用如下函数(注:ch只能是单字符,如键盘上英文状态下的字符)

function SplitString(const Source,ch:String):TStringList;

var

temp:String;

i:Integer;

begin

Result:=;

//如果是空自符串则返回空列表

if Source=''

then exit;

temp:=Source;

i:=pos(ch,Source);

while i<>0 do

begin

(copy(temp,0,i-1));

Delete(temp,1,i);

i:=pos(ch,temp);

end;

(temp);

end;

二、直接使用TStringList

procedure 3Click(Sender: TObject);

var

Str:String;

ResultList:TStringList;

I:Integer;

begin

str:= '南京~信息~工程~大学';

ResultList := ;

try

ter := '~';

tedText := str;

for I:= 0 to -1 do

begin

(s[I]);

end;

finally

FreeAndNil(ResultList);

end;

end;

三、支持特殊字符版(ch可以为字符串,如'aa')

function SplitString(const Source,ch:String):TStringList;

var

Temp:String;

I:Integer;

chLength:Integer;

begin

Result:=;

//如果是空自符串则返回空列表

if Source='' then Exit;

Temp:=Source;

I:=Pos(ch,Source);

chLength := Length(ch);

while I<>0 do

begin

(Copy(Temp,0,I-chLength+1));

Delete(Temp,1,I-1 + chLength);

I:=pos(ch,Temp);

end;

(Temp);

end;

[delphi函数] ChangeFileExt 更改文件的后缀扩展名

函数说明 更改指定文件的扩展名,函数原型如下:

delphi中源码

function ChangeFileExt(const FileName, Extension: string): string;

//第一个参数为要修改的文件名,可以带路径

//第二个参数为修改后的后缀名

//该函数返回修改后的文件名

var

I: Integer;

begin

I := LastDelimiter('.' + PathDelim + DriveDelim,Filename);

if (I = 0) or (FileName[I] <> '.') then I := MaxInt;

Result := Copy(FileName, 1, I - 1) + Extension;

end;

比如:

s:=changefileext('f:','.ini');

showmessage(s);//f:

=============

function ChangeFileExt(const FileName, Extension: string): string;

var

I: Integer;

begin

I := LastDelimiter('.' + PathDelim + DriveDelim,Filename);

if (I = 0) or (FileName[I] <> '.') then I := MaxInt;

Result := Copy(FileName, 1, I - 1) + Extension;

end;

delphi函数] GetDir 获取指定驱动器的当前路径名

//获取指定驱动器的当前路径名 GetDir

var

dir: string;

b: Byte;

begin

b := 0;

GetDir(b,dir);

ShowMessage(dir); //

//第一个参数: 1、2、3、4...分别对应: A、B、C、D...

//0 是缺省驱动器

end;

delphi函数] GetFileVersion 获取当前文件的版本号

//获取当前文件的版本号 GetFileVersion

var

s: string;

i: Integer;

begin

s := 'C:';

i := GetFileVersion(s); //如果没有版本号返回 -1

ShowMessage(IntToStr(i)); //327681 这是当前记事本的版本号(还应该再转

换一下)

end;

===============

function GetFileVersion(const AFileName: string): Cardinal;

var

FileName: string;

InfoSize, Wnd: DWORD;

VerBuf: Pointer;

FI: PVSFixedFileInfo;

VerSize: DWORD;

begin

Result := Cardinal(-1);

// GetFileVersionInfo modifies the filename parameter data while parsing.

// Copy the string const into a local variable to create a writeable copy.

FileName := AFileName;

UniqueString(FileName);

InfoSize := GetFileVersionInfoSize(PChar(FileName), Wnd);

if InfoSize <> 0 then

begin

GetMem(VerBuf, InfoSize);

try

if GetFileVersionInfo(PChar(FileName), Wnd, InfoSize, VerBuf)

then

if VerQueryValue(VerBuf, '', Pointer(FI), VerSize) then

Result:= VersionMS;

finally

FreeMem(VerBuf);

end;

end;

end;

delphi函数] FileSearch 查找一个文件

//查找一个文件 FileSearch

var

FileName,Dir,s: string;

begin

FileName := '';

Dir := 'c:windows';

s := FileSearch(FileName,Dir);

if s<>'' then

ShowMessage(s) //c:

else

ShowMessage('没找到');

end;

=================

function FileSearch(const Name, DirList: string): string;

var

I, P, L: Integer;

C: Char;

begin

Result := Name;

P := 1;

L := Length(DirList);

while True do

begin

if FileExists(Result) then Exit;

while (P <= L) and (DirList[P] = PathSep) do Inc(P);

if P > L then Break;

I := P;

while (P <= L) and (DirList[P] <> PathSep) do

begin

if DirList[P] in LeadBytes then

P := NextCharIndex(DirList, P)

else

Inc(P);

end;

Result := Copy(DirList, I, P - I);

C := AnsiLastChar(Result)^;

if (C <> DriveDelim) and (C <> PathDelim) then

Result := Result + PathDelim;

Result := Result + Name;

end;

Result := '';

end;

delphi函数] DiskSize、DiskFree 获取磁盘空间

//获取磁盘空间 DiskSize; DiskFree

var

r: Real;

s: string;

begin

r := DiskSize(3); //获取C:总空间, 单位是字节

r := r/1024/1024/1024;

Str(r:0:2,s); //格式为保留两位小数的字符串

s := 'C盘总空间是: ' + s + ' GB';

ShowMessage(s); // GB

r := DiskFree(3); //获取C:可用空间

r := r/1024/1024/1024;

Str(r:0:2,s);

s := 'C盘可用空间是: ' + s + ' GB';

ShowMessage(s); // GB

end;

=====================

function DiskSize(Drive: Byte): Int64;

var

FreeSpace: Int64;

begin

if not InternalGetDiskSpace(Drive, Result, FreeSpace) then

Result := -1;

end;

{$ENDIF}

function DiskFree(Drive: Byte): Int64;

var

TotalSpace: Int64;

begin

if not InternalGetDiskSpace(Drive, TotalSpace, Result) then

Result := -1;

end;

delphi函数] FileGetAttr、FileSetAttr 读取与设置文件属性

//读取与设置文件属性 FileGetAttr; FileSetAttr

var

FileName: string;

Attr: Integer; //属性值是一个整数

begin

FileName := 'c:';

Attr := FileGetAttr(FileName);

ShowMessage(IntToStr(Attr)); //32, 存档文件

//设置为隐藏和只读文件:

Attr := FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN;

if FileSetAttr(FileName,Attr)=0 then //返回0表示成功

ShowMessage('设置成功!');

//属性可选值(有些用不着):

//FILE_ATTRIBUTE_READONLY = 1; 只读

//FILE_ATTRIBUTE_HIDDEN = 2; 隐藏

//FILE_ATTRIBUTE_SYSTEM = 4; 系统

//FILE_ATTRIBUTE_DIRECTORY = 16

//FILE_ATTRIBUTE_ARCHIVE = 32; 存档

//FILE_ATTRIBUTE_DEVICE = 64

//FILE_ATTRIBUTE_NORMAL = 128; 一般

//FILE_ATTRIBUTE_TEMPORARY = 256

//FILE_ATTRIBUTE_SPARSE_FILE = 512

//FILE_ATTRIBUTE_REPARSE_POINT = 1204

//FILE_ATTRIBUTE_COMPRESSED = 2048; 压缩

//FILE_ATTRIBUTE_OFFLINE = 4096

//FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192; 不被索引

//FILE_ATTRIBUTE_ENCRYPTED = 16384

end;

===================

function FileGetAttr(const FileName: string): Integer;

begin

Result := GetFileAttributes(PChar(FileName));

end;

function FileSetAttr(const FileName: string; Attr: Integer): Integer;

begin

Result := 0;

if not SetFileAttributes(PChar(FileName), Attr) then

Result := GetLastError;

end;

{$ENDIF}

function FileSetAttr(const FileName: string; Attr: Integer): Integer;

begin

Result := 0;

if not SetFileAttributes(PChar(FileName), Attr) then

Result := GetLastError;

end;

{$ENDIF}

delphi函数] FileAge 获取文件的创建时间

function FileAge(const FileName: string): Integer;

{$IFDEF MSWINDOWS}

var

Handle: THandle;

FindData: TWin32FindData;

LocalFileTime: TFileTime;

begin

Handle := FindFirstFile(PChar(FileName), FindData);

if Handle <> INVALID_HANDLE_VALUE then

begin

ose(Handle);

if (Attributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then

begin

FileTimeToLocalFileTime(WriteTime, LocalFileTime);

if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,

LongRec(Result).Lo) then Exit;

end;

end;

Result := -1;

end;

{$ENDIF}

{$IFDEF LINUX}

var

st: TStatBuf;

begin

if stat(PChar(FileName), st) = 0 then

Result := _mtime

else

Result := -1;

end;

{$ENDIF}

本文标签: 函数字符串格式类型