对于编程人员来讲,熟练掌这个函数在编程时会节省很多麻烦
1.Copy:该函数用于从字符串中复制指定范围中的字符。
原型:
function Copy(S; Index, Count: Integer): string; //参数一:S; 是数据源; //参数二:Index; 数据源中的某一处开始复制; //参数三:Count;这个是要复制字符串的长度(即个数);
//返回的是一个字符类型,也就是截取的字符; procedure TForm1.Button1Click(Sender: TObject); const ms = 'Delphi'; begin ShowMessage(Copy(ms,1,1));
{最后显示的结果是:"D" 因为我们是从第一个字母开始,第一个字母结束所以就是“D”}
end;
2.Concat:这个函数很简单功能就是将多个字符串连接为一个字符串;
原型:
function Concat(s1 [, s2,..., sn]: string): string; //参数:s1,[,s2,...,sn]:string 这个函数的参数就是N个单独的字符串;
//最后返回的是连接起来的字符串; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(Concat('程','序','人','生','Delphi')); { 这里我将程序人生Delphi分开为五个单独的字符串,然后就用Concat这个函数 再将他们连接为一个完整的句子。在看到这个功能后大家是不是和我一样想到这个“+” 呵 呵 ! 最后显示的结果: 程序人生Delphi } end;
3.Insert:插入一个字符或是字符串到指定的字符或是字符串中;
原型:
procedure Insert(Source: string; var S: string; Index: Integer);
//参数1:Source:要插入的字符或是字符串;
//参数2:S 是被插入的字符或字符串;
//参数3:插入点; 例子: procedure TForm1.Button1Click(Sender: TObject); var S: string; begin S := 'Honest Lincoln'; Canvas.TextOut(10, 10, S); Insert('Abe ', S, 8); Canvas.TextOut(10, 40, S); {最后输出的是:Honest Abe Lincoln} end;
4.Delete:功能删除字符串指定的字符或字符串:
原型:
procedure Delete(var S: string; Index: Integer; Count: Integer); //参数1:S:为删除源; //参数2:为从那个地方开始删除(空格也算在内); //参数3:为结束点;
//这个样 参数2和参数3就构成了一个删除的范围; 例子: procedure TForm1.Button1Click(Sender: TObject); var s:String; begin s:='Delphi 2007 XE2'; Canvas.TextOut(10, 10, s); end; procedure TForm1.Button2Click(Sender: TObject); var s:String; begin s:='Delphi 2007 XE2'; Delete(s,7,4); Canvas.TextOut(10, 30 ,s); end;
end.
5.High:返回数组下标的最大值;
原型:
procedure High(X); //参数1:x 要返回的数组下标的最大值; 例子: procedure TForm1.Button2Click(Sender: TObject); var testarr:array[0..99] of Integer ; begin ShowMessage(IntToStr(High(testarr))); {这个要转换一个因为我声明的是一个整数类型的数组,所以要将他转换成字符类类型的再输出。} end;
6.Low:返回数据上标的最小值;
原型:
procedure Low(x); //参数1:返回数据上标的最小值; 例子: procedure TForm1.Button2Click(Sender: TObject); var testarr:array[0..99] of Integer ; begin ShowMessage(IntToStr(Low(testarr)));
end; end.
7.LeftStr:返回字符串左边指定的字符或字符串。
原型:
function LeftStr(const AText: AnsiString; const ACount: Integer): AnsiString; overload; function LeftStr(const AText: WideString; const ACount: Integer): WideString; overload; //参数1:指定完整字符串; //参数2:指定截取的个数;
例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,StrUtils; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Edit2: TEdit; Label1: TLabel; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(LeftStr(Edit1.Text,StrToInt(Edit2.Text))); end; procedure TForm1.FormCreate(Sender: TObject); begin Label1.Caption :='测试字符&&字符串:'; Label2.Caption :='参数1:'; Button1.Caption:='测试'; Edit1.Text :='Delphi 2009'; Edit2.Text :=''; end; end.
原型:
Function Length(s):integer; //参数:指定要返回的字符长度的字符串&&字符;
例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,StrUtils; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(Length(Edit1.Text))); //这里这个函数返回的是一个整形; //所以我们将它转换成字符串类型后可以将他输出了!; end; procedure TForm1.FormCreate(Sender: TObject); begin Label1.Caption :='测试字符&&字符串:'; Button1.Caption:='测试'; Edit1.Text :='Delphi 2009'; end; end.
9.LowerCase:将字符串中的英文字符转换为小写字母;
原型:
function LowerCase(const S: string): string; overload; //参数:要转换的字符(英文字符); 例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,StrUtils; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(LowerCase(Edit1.Text)); end; procedure TForm1.FormCreate(Sender: TObject); begin Label1.Caption :='测试字符&&字符串:'; Button1.Caption:='测试'; Edit1.Text :='DELPHI 2009'; //显示结果:delphi 2009; end; end.
10.UpperCase:和上面LowerCase相反;
原型:
function UpperCase(const S: string): string; overload; //参数:要转换的字符(英文字符); 例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,StrUtils; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(UpperCase(Edit1.Text)); end; procedure TForm1.FormCreate(Sender: TObject); begin Label1.Caption :='测试字符&&字符串:'; Button1.Caption:='测试'; Edit1.Text :='delphi 2009'; //显示结果:DELPHI 2009; end; end.
11.MidStr:(所在单元:StrUtils)返回指定范围字符&&字符串;
原型:
function MidStr(const AText: AnsiString; const AStart, ACount: Integer): AnsiString; overload; //参数1:要截取的字符&&字符串; //参数2:截取的开始点 ; //参数3:截取的结束点; unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,StrUtils; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption :='MidStr测试'; Button2.Caption :='MidStr测试二'; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(MidStr('Delphi',1,3)); end; procedure TForm1.Button2Click(Sender: TObject); begin ShowMessage(MidStr('程序人生',1,2)); 0 end; { 说明:这个函数和前面介绍的Copy这个函数相对来说要比Copy这个函数好用的多,因为这个函数 他可以支持中文,而前面的那个函数是不支持中文的!
显示结果:
MidStr测试:Del
MidStr测试二:程序 } end.
12.Pos:功能:查找字符串;
原形:
function Pos(Substr: string; S: string): Integer; { 参数1:这个是被查看的字符&&字符串; 参数2:查找的字符 &&字符串; 说明:如果查找到要查找的 字符 &&字符串这返回 1,如果没有查找到则返回 0; 说明二:如果是查找汉字的话则就用AnsiPos这个函数,因为这个函数他支持汉字的查找; }
例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormActivate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption:='测试Pos'; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(Pos(Edit1.Text,Edit2.Text))); end; procedure TForm1.FormActivate(Sender: TObject); begin end; end.
原型:
function RightStr(const AText: AnsiString; const ACount: Integer): AnsiString; overload; { 参数1:要截取的源字符串; 参数2:显示的指定的个数; 返回的是一个字符类型; } 例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,StrUtils; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var i:string; begin i:='Delphi 2009'; ShowMessage(RightStr(i,4)); { 最后显示的结果是:2009,如果将4换成5的话就会显示 2009; } end; end.
原型:
procedure SetLength(var S; NewLength: Integer); { 参数1:字符变量或是动态数组; 参数2:字符变量或是动态数组大小; } 例子: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var i:string; begin I:='Delphi 2009'; SetLength(i,3); ShowMessage(i); { 最后显示结果是:Del 上面的那句我们也可以将他翻译成如下: i:String[3] ; beign i:='Delphi 2009'; ShowMessage(i); } end; procedure TForm1.Button2Click(Sender: TObject); var TestArr:array of String; i,s:integer; begin SetLength(TestArr,10); for I := 0 to 10 do begin for s:=0 to 10 do begin TestArr[i]:=IntToStr(i); end; ShowMessage(IntToStr(i)); end; { 在这里我将那个动态数组设置成了长度为0.。9 大小的数组 所以下面我将第二个循环设置为10 但是这个数组的大小只能 存放10个大小的字符,当第十个字符显示完后,就会弹出一个 错误; } end; procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption :='设置字符串长度'; Button2.Caption :='设计动态数组大小'; end; end.
原型:
function StrPCopy(Dest: PAnsiChar; const Source: AnsiString): PAnsiChar; overload; { 参数1:目标数组; 参数2:要写入的字符串&&字符; }
例子:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var arrchar:array[0..100] of Char; begin StrPCopy(arrchar,'delphi2009'); ShowMessage(arrchar); { 最后显示结果:delphi2009 ; } end; end.
有所不同的是第一个Trim是去掉所有的空格,第二个TrimRight这个是去掉左边的所有空格
第三个那一定就是去掉右边所有空格;关于这三人函数我就简单的举个例子;
例子:
var
i:string;
begin
i:=' Delphi 2009 ';
ShowMessage(Trim(i));
ShowMessage(TrimRight(i));
ShowMessage(TrimLeft(i));
end;
{
最后显示的结果:
1.Trim: ’Delphi2009‘
2.TrimRight: ‘Delphi 2009 ’
3.TrimLeft: ‘ Delphi 2009’
}
上面那些函数就是我们常用的一些函数了,如果大家认为对自己有帮助的话,请回帖;