| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 27408 人关注过本帖, 5 人收藏
标题:[分享]新手和CN必看
只看楼主 加入收藏
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
Randomize 乱数种子.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Randomize;
Example
var

I: Integer;
begin
Randomize;
for I := 1 to 50 do begin
{ Write to window at random locations }
Canvas.TextOut(Random(Width), Random(Height), 'Boo!');
end;
end;
##Randomize, Random Example
-----------------------------------------------------------------------------
SizeOf 传回X变数的位元数.
-----------------------------------------------------------------------------
Unit System
函数原型 function SizeOf(X): Integer;
范例 type
CustRec = record
Name: string[30];
Phone: string[14];
end;
var
P: ^CustRec;
begin
GetMem(P, SizeOf(CustRec));
Canvas.TextOut(10, 10, 'The size of the record is ' +
IntToStr(SizeOf(CustRec)));
FreeMem (P, SizeOf(CustRec));
Readln;
end;
-----------------------------------------------------------------------------
Swap 将一组变数的高低位元交换.
-----------------------------------------------------------------------------
Unit System
函数原型 function Swap(X);
范例 var
X: Word;
begin
X := Swap($1234); { $3412 }
end;
-----------------------------------------------------------------------------
UpCase 将一字元转为大写字母.
-----------------------------------------------------------------------------
Unit System
函数原型 function UpCase(Ch: Char): Char;
范例 uses Dialogs;
var
s : string;
i : Integer;
begin

{ Get string from TEdit control }
s := Edit1.Text;
for i := 1 to Length(s) do
s[i] := UpCase(s[i]);
MessageDlg('Here it is in all uppercase: ' + s, mtInformation,
[mbOk], 0);
end;
Example
var

s : string;
i : Integer;
begin
{ Get string from TEdit control }
s := Edit1.Text;
for i := 1 to Length(s) do
if i mod 2 = 0 then s[i] := UpCase(s[i]);
Edit1.Text := s;
end;

===========================================
Ordinal routines 序列常式
==========================================
Dec 使变数递减.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Dec(var X[ ; N: Longint]);
说明 Dec(X) ==> X:=X-1;
Dec(X,N) ==> X:=X-N;
范例 var
IntVar: Integer;
LongintVar: Longint;
begin
Intvar := 10;
LongintVar := 10;
Dec(IntVar); { IntVar := IntVar - 1 }
Dec(LongintVar, 5); { LongintVar := LongintVar - 5 }
end;
-----------------------------------------------------------------------------
Inc 使变数递增.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Inc(var X [ ; N: Longint ] );
说明 Inc(X) ==> X:=X-1;

Inc(X,N) ==> X:=X-N;
范例 var
IntVar: Integer;
LongintVar: Longint;
begin
Inc(IntVar); { IntVar := IntVar + 1 }
Inc(LongintVar, 5); { LongintVar := LongintVar + 5 }
end;



情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:44
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
Odd 检查是否为奇数.
-----------------------------------------------------------------------------
Unit System
函数原型 function Odd(X: Longint): Boolean;
Example
begin

if Odd(5) then
Canvas.TextOut(10, 10, '5 is odd.')
else
Canvas.TextOut(10, 10, 'Something is odd!');
end;
=======================================
Pointer and address routines 位址常式
=======================================
Addr 传回一个物件的位址.
-----------------------------------------------------------------------------
Unit System
函数原型 function Addr(X): Pointer;
Example
var
I : Integer;
NodeNumbers: array [0 .. 100] of Integer;
begin
with TreeView1 do
begin
for I := 0 to Items.Count - 1 do
begin
NodeNumbers[I] := CalculateValue(Items[I]);
Items[I].Data := Addr(NodeNumber[I]);
end;
end;
end;
-----------------------------------------------------------------------------
Assigned 测试指标变数是否为nil.

-----------------------------------------------------------------------------
Unit System
函数原型 function Assigned(var P): Boolean;
说明 当@P=nil ==> 传回FALSE
范例 var P: Pointer;
begin
P := nil;
if Assigned (P) then
Writeln ('You won't see this');
GetMem(P, 1024); {P valid}
FreeMem(P, 1024); {P no longer valid and still not nil}
if Assigned (P) then
Writeln ('You'll see this');
end
===================================
String-formatting routines 字串格式化
==================================
FmtStr 格式化.
-----------------------------------------------------------------------------
FmtStr(var StrResult: string;const Format: string;const Args: array of string );

-----------------------------------------------------------------------------
Format
Format(const Format: string;const Args: array of string ): string;
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 procedure FmtStr(var Result: string; const Format: string;
const Args: array of const);
function Format(const Format: string; const Args: array of
const): string;
说明 %d : 整数
%e : 科学式
%f : 定点实数
%g : 实数
%n : 实数(-d,ddd,ddd.dd ...)
%m: 金钱格式
%p : point
%s : 字串
%x : Hex
范例 var
i: Integer;
j: Double;
s: String;
t: String;

begin
t:=Format('%d %8.2f %s',[i,j,s]);
ListBox1.Item.Add(t);
end;

BubbleSeries1.PercentFormat := '##0.0# %';
Example
procedure TForm1.Table1AfterDelete(DataSet: TDataSet);
begin
StatusBar1.SimpleText := Format('There are now %d records in the table', [DataSet.RecordCount]);
end;

S:= Format( '1-? ??????? ??????? - %d, 2-? - %d, 3-? - %d', [10,20,30] );




Format( '%*.*f', [ 9, 2, 12345.6789 ] );
Format( '%9.2f', [ 12345.6789 ] );
Format( '%3d, %d, %0:d, %2:-4d, %d', [ 1, 2, 3, 4 ] );
' 1,2,1,3 ,4'


## AfterDelete, Format Example


=======================================
String-handling routines (Pascal-style) 字串函式
=======================================
AnsiCompareStr 比较两个字串的大小.依安装的 language driver.
-----------------------------------------------------------------------------
AnsiCompareText ( AnsiCompareText 此项不分大小写 ).
-----------------------------------------------------------------------------
Unit SysUtils
var

S1,S2: string;
I: Integer;

begin

S1:= 'A????';
S2:= '?????';

I:= CompareStr(S1, S2); { I = 0, ?.?. S1 = S2 }
if I=0 then
MessageDlg(S1, '=', S2, mtWarning, [mbOK], 0);
end;

函数原型 function AnsiCompareStr(const S1, S2: string):Integer;
函数原型 function AnsiCompareText(const S1, S2: string):Integer;


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:45
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
AnsiLowerCase 将字串全部转为小写字母.依安装的 language driver.
-----------------------------------------------------------------------------
AnsiUpperCase 将字串全部转为大写字母.依安装的 language drive
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function AnsiLowerCase(const S: string): string;
函数原型 function AnsiUpperCase(const S: string): string;
-----------------------------------------------------------------------------
CompareStr 比较两个字串的大小.
-----------------------------------------------------------------------------
CompareText ( CompareText 此项不分大小写 ).
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function CompareStr(const S1, S2: string): Integer;
函数原型 function CompareText(const S1, S2: string): Integer;
范例 var
String1, String2 : string;
I : integer;
begin
String1 := 'STEVE';
String2 := 'STEVe';
I := CompareStr(String1, String2); { I < 0 }
if I < 0 then
MessageDlg('String1 < String2', mtWarning, [mbOK], 0);
end;

var
String1, String2 : string;
I : integer;
begin
String1 := 'ABC';
String2 := 'aaa';
I := CompareStr(String1, String2); { I < 0 }
if I < 0 then
MessageDlg(' String1 < String2', mtWarning, [mbOK], 0);
end;

Examlpe
var ColumnToSort: Integer;

The OnColumnClick event handler sets the global variable to indicate the column to sort and calls AlphaSort:

procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);

begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;

The OnCompare event handler causes the list view to sort on the selected column:

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
var
ix: Integer;
begin
if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;
end;
## OnColumnClick, AlphaSort, OnCompare, CompareText example
-----------------------------------------------------------------------------
Concat 将字串相加.
-----------------------------------------------------------------------------
Unit System
函数原型 function Concat(s1 [, s2,..., sn]: string): string;
说明 与 S := S1 + S2 + S3 ...; 相同.
范例 var
S: string;
begin
S := Concat('ABC', 'DEF'); { 'ABCDE' }
end;

var
S: string;
begin
S:= '? '+ '???? '+ '???????? ??????';

S:= Concat('? ', '???? ', '???????? ??????');
// ? ????? ??????? S := '? ???? ???????? ??????'
end;
-----------------------------------------------------------------------------
Copy 从母字串拷贝至另一个字串.
-----------------------------------------------------------------------------
Unit System
函数原型 function Copy(S: string; Index, Count: Integer): string;
说明 S : 字串.
Indexd : 从第几位开始拷贝.
Count : 总共要拷贝几位.
范例 var S: string;
begin
S := 'ABCDEF';
S := Copy(S, 2, 3); { 'BCD' }
end;
----------------
var
S: string;
begin
S:= '??????';
S:= Copy( S, 3, 4); // S := '????'
end;
---------------
Example
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var
Found: boolean;
i,SelSt: Integer;
TmpStr: string;
begin
{ first, process the keystroke to obtain the current string }
{ This code requires all items in list to be uppercase}
if Key in ['a'..'z'] then Dec(Key,32); {Force Uppercase only!}
with (Sender as TComboBox) do
begin
SelSt := SelStart;
if (Key = Chr(vk_Back)) and (SelLength <> 0) then
TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255)

else if Key = Chr(vk_Back) then {SelLength = 0}
TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255)
else {Key in ['A'..'Z', etc]}
TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255);

if TmpStr = ' then Exit;
{ update SelSt to the current insertion point }

if (Key = Chr(vk_Back)) and (SelSt > 0) then Dec(SelSt)

else if Key <> Chr(vk_Back) then Inc(SelSt);
Key := #0; { indicate that key was handled }
if SelSt = 0 then
begin
Text:= ';
Exit;
end;

{Now that TmpStr is the currently typed string, see if we can locate a match }

Found := False;
for i := 1 to Items.Count do
if Copy(Items[i-1],1,Length(TmpStr)) = TmpStr then
begin
Text := Items[i-1]; { update to the match that was found }
ItemIndex := i-1;
Found := True;
Break;
end;
if Found then { select the untyped end of the string }
begin
SelStart := SelSt;
SelLength := Length(Text)-SelSt;

end
else Beep;
end;
end;
-----------------------
procedure TComponentEditor.Copy;
var
AFormat : Word;
AData,APalette : THandle;
begin
with Component as TImage do
begin
Picture.SaveToClipBoardFormat(AFormat,AData,APalette);
ClipBoard.SetAsHandle(AFormat,AData);
end;
end;


## Copy, Chr, SelStart, SelLength example




情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:46
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
Delete 删除字串中的数个字元.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Delete(var S: string; Index, Count:Integer);
说明 S : 字串.
Indexd : 从第几位开始删.
Count : 总共要删几位.
范例 var
s: string;
begin
s := 'Honest Abe Lincoln';
Delete(s,8,4);
Canvas.TextOut(10, 10, s); { 'Honest Lincoln' }
end;
var
S: string;
begin
S:= '???????, ??????, ??????????!';
Delete(S, 8, 1); // S := '??????? ??????, ??????????!'
MessageDlg(S, mtWarning, [mbOK],0);
end;
-----------------------------------------------------------------------------
NewStr 在 heap 中配置一个新的字串空间给PString 指标.
-----------------------------------------------------------------------------
DisposeStr 在 heap 中释放一个字串空间 PString指标.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function NewStr(const S: string): PString;
函数原型 procedure DisposeStr(P: PString);
说明 S : 字串.
Pstring : 新的字串指标.
范例 var
P: PString;
S: string;
begin
S := 'Ask me about Blaise';
P := NewStr(S);
DisposeStr(P):

end;
-----------------------------------------------------------------------------
Insert 将一个子字串插入另一个字串中.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Insert(Source: string; var S: string; Index: Integer);
说明 Source : 子字串.
S : 被插入的母字串.
Indexd : 从第几位开始插入.
范例 var
S: string;
begin
S := 'Honest Lincoln';
Insert('Abe ', S, 8); { 'Honest Abe Lincoln' }
end;
var
S: string;
begin
S:= '??????? ?????? ??????????.';
Insert( '!', S, 8); { S := '???????! ?????? ??????????.'}
MessageDlg( S, mtWarning, [mbOK],0);
end;
-----------------------------------------------------------------------------
IntToHex 将 Int 转为 Hex.
-----------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);

var
i: Integer;
begin
Label1.Caption := ';
for i := 1 to Length(Edit1.Text) do
begin
try
Label1.Caption := Label1.Caption + IntToHex(Edit1.Text[i],4) + ' ';
except
Beep;
end;
end;
end;

Exam:

Edit2.text:=(strtoint(Edit1.text),6);

-----------------------------------------------------------------------------
IntToStr 将 Int 转为 Str.
-----------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
try
Label1.Caption := IntToStr(StrToInt(Edit1.Text) * StrToInt(Edit2.Text));
except
ShowMessage('You must specify integer values. Please try again.');
end;
end;
-----------------------------------------------------------------------------
StrToInt 将 Str 转为 Int.
-----------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
J: Integer;
begin
I := StrToInt(Edit1.Text);
J := StrToInt(Edit2.Text);
ShowMessage(IntToStr(I + J));
end;
-----------------------------------------------------------------------------
StrToIntDef 将 Str 转为 Int.当转换有误时,则传回 Default 的值.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function IntToHex(Value: Integer; Digits: Integer): string;
函数原型 function IntToStr(Value: Integer): string;
函数原型 function StrToInt(const S: string): Integer;
函数原型 function StrToIntDef(const S: string; Default: Integer): Integer;
说明 Value : 欲转换的整数.
Digits : 欲转换为几位数的 Hex.
范例 procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := IntToHex(StrToInt(Edit1.Text), 6);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Value: Integer;
begin
Value := 1234;
Edit1.Text := IntToStr(Value);

end;

procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
I: Integer;
begin
S := '22467';
I := StrToInt(S);
Inc(I);
Edit1.Text := IntToStr(I);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
NumberString: string;
Number: Integer;
begin
NumberString := Edit1.Text;
Number := StrToIntDef(NumberString, 1000);
Edit2.Text := IntToStr(Number);
end;
Example
var

I: Integer;
ListItem: string;
begin
for I := 0 to Query1.ParamCount - 1 do
begin
ListItem := ListBox1.Items[I];
case Query1.Params[I].DataType of
ftString:
Query1.Params[I].AsString := ListItem;
ftSmallInt:
Query1.Params[I].AsSmallInt := StrToIntDef(ListItem, 0);
ftInteger:
Query1.Params[I].AsInteger := StrToIntDef(ListItem, 0);
ftWord:
Query1.Params[I].AsWord := StrToIntDef(ListItem, 0);

ftBoolean:
begin
if ListItem = 'True' then

Query1.Params[I].AsBoolean := True
else
Query1.Params[I].AsBoolean := False;
end;
ftFloat:
Query1.Params[I].AsFloat := StrToFloat(ListItem);
ftCurrency:
Query1.Params[I].AsCurrency := StrToFloat(ListItem);
ftBCD:
Query1.Params[I].AsBCD := StrToCurr(ListItem);
ftDate:

Query1.Params[I].AsDate := StrToDate(ListItem);
ftTime:
Query1.Params[I].AsTime := StrToTime(ListItem);
ftDateTime:
Query1.Params[I].AsDateTime := StrToDateTime(ListItem);
end;
end;
end;
--------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
Number: Integer;
begin
Number := StrToIntDef(Edit1.Text, 1000);
Edit2.Text := IntToStr(Number);
end;
-------------------
## ParamCount, DataType, StrToIntDef, AsXXX Example


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:47
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
Str 将数值转换为格式化的字串.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Str(X [: Width [: Decimals ]]; var S);
说明 X : 欲转换的整数 or 实数.
Width : 格式化长度.(Integer)
Decimals : 小数点位数.(Integer)
范例 function MakeItAString(I: Longint): string;
{ Convert any integer type to a string }
var
S: string[11];
begin
Str(I, S);
MakeItAString:= S;
end;
begin
Canvas.TextOut(10, 10, MakeItAString(-5322));
end;

-----------------------------------------------------------------------------
Val 将字串转为数字.
-----------------------------------------------------------------------------
Unit System
函数原型 procedure Val(S; var V; var Code: Integer);
说明 S : 欲转换的字串.
V : 转换後的整数 or 实数.
Code : Code = 0 表示转换成功.
范例 uses Dialogs;
var
I, Code: Integer;
begin
{ Get text from TEdit control }
Val(Edit1.Text, I, Code);
{ Error during conversion to integer? }
if code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code),
mtWarning, [mbOk], 0);
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
Readln;
end;
-----------------------------------------------------------------------------
Length 字串长度.
-----------------------------------------------------------------------------
Unit System
函数原型 function Length(S: string): Integer;
说明 S : 欲转换的字串.
范例 var
S: string;
begin
S := 'The Black Knight';
Canvas.TextOut(10, 10, 'String Length = ' +
IntToStr(Length(S)));
end;
Example
procedure TForm1.Button1Click(Sender: TObject);

var
i: Integer;
begin
Label1.Caption := ';
for i := 1 to Length(Edit1.Text) do
begin
try
Label1.Caption := Label1.Caption + IntToHex(Edit1.Text[i],4) + ' ';
except
Beep;
end;
end;
end;

范例
procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
begin
S := memo1.text;
Label1.caption :=' ' + IntToStr(Length(S));
end;

var
S: string;
I: Integer;
begin
S:= '? ???? ???????? ??????';
I:= Length(S); // I:= 22
MessageDlg( '????? ??????='+ IntToStr(I), mtWarning, [mbOK], 0);
end;
## Length, IntToHex Example


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:48
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
Pos 寻找子字串在母字串中的位置.
-----------------------------------------------------------------------------
Unit System
函数原型 function Pos(Substr: string; S: string): Integer;
说明 Substr : 子字串.
S : 母字串.
范例
procedure TForm1.Button1Click(Sender: TObject);
var S: string;
begin
S := ' 1234.5 ';
{ Convert spaces to zeroes }
while Pos(' ', S) > 0 do
S[Pos(' ', S)] := '0';
Label1.Caption := S;
Label1.Font.Size := 16;
end;

var
S: string;
I: Integer;
begin
S:= '? ???? ???????? ??????';
I:= Pos( '???', S); // I:= 3
end;
//DEMO 001234.50 //空白字串补零
-----------------------------------------------------------------------------
LowerCase 将字串全部转为小写字母.
-----------------------------------------------------------------------------
Unit System
函数原型 function LowerCase(const S: string): string;
范例 procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text := LowerCase(Edit1.Text);
end;
Example
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := LowerCase(Edit1.Text);
end;
var
S: string;
begin
S:= LowerCase( '????????.TXT') ; // S := '????????.txt'
end;
-----------------------------------------------------------------------------
UpperCase 将字串全部转为大写字母.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function UpperCase(const S: string): string;
范例 procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ListBox1.Items.Count -1 do
ListBox1.Items[I] := UpperCase(ListBox1.Items[I]);
end;
Example
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ListBox1.Items.Count -1 do
ListBox1.Items[I] := UpperCase(ListBox1.Items[I]);
end;
-----------------------------------------------------------------------------
Trim 将字串前後的空白及控制字元清掉.
Trim ( const S: string ): string;
SysUtils
var
S: string;
L: Integer;
begin
S:= #13' ???! '#13;
L:= length( S); // L := 10
S:= Trim( S); // S := '???!'
L:= L-length( S); // L := 5
MessageDlg( '??????? ???????? - '+ IntToStr(L), mtInformation, [mbOk], 0);
end;

-----------------------------------------------------------------------------
TrimLeft 将字串左边的空白及控制字元清掉.
SysUtils
var
S: string;
L: Integer;
begin
S:= #13' ???! '#13;
L:= length( S); // L := 10
S:= TrimLeft( S); // S := '???! '#13
L:= L-length( S); // L := 3
MessageDlg( '??????? ???????? - '+IntToStr(L), mtInformation, [mbOk], 0);
end;


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:48
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
TrimRight 将字串右边的空白及控制字元清掉.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function Trim(const S: string): string;
函数原型 function TrimLeft(const S: string): string;
函数原型 function TrimRight(const S: string): string;

var
S: string;
L: Integer;
begin
S:= #13' ???! '#13;
L:= length( S); // L := 10
S:= TrimRight( S); // S := #13' ???!'
L:= L-length( S); // L := 2
MessageDlg( '??????? ???????? - '+IntToStr(L), mtInformation, [mbOk], 0);
end;
-----------------------------------------------------------------------------
AdjustLineBreaks 将字串的换行符号全部改为#13#10
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function AdjustLineBreaks(const S: string): string;

=======================================
String-handling routines (null-terminated)字串函式
=======================================
StrAlloc 配置字串空间.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrAlloc(Size: Cardinal): PChar;
说明 Size=字串最大空间+1
-----------------------------------------------------------------------------
StrBufSize 传回由 StrAlloc 配置空间的大小
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrBufSize(Str: PChar): Cardinal;


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:48
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
StrCat 字串相加.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrCat(Dest, Source: PChar): PChar;
范例 uses SysUtils;
const
Obj: PChar = 'Object';
Pascal: PChar = 'Pascal';
var
S: array[0..15] of Char;
begin
StrCopy(S, Obj);
StrCat(S, ' ');
StrCat(S, Pascal);
Canvas.TextOut(10, 10, StrPas(S));
end;
Example
procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: PChar;
begin
GetMem(Buffer,Length(Label1.Caption) + Length(Edit1.Text) + 1);
StrCopy(Buffer, PChar(Label1.Caption));
StrCat(Buffer, PChar(Edit1.Text));
Label1.Caption := Buffer;
Edit1.Clear;
FreeMem(Buffer);
end;

const
P0: PChar = '??????-';
P1: PChar = '??????????';
P2: PChar = '????????';
var
S1, S2: array[0..20] of Char;
begin
StrCopy(S1, P0);
StrCopy(S2, P0);
StrCat(S1, P1); { S1 := '??????-??????????' }
StrCat(S2, P2); { S2 := '??????-????????' }
MessageDlg( S1+ #13+ S2, mtInformation, [mbOk], 0);
end;

##StrCopy, StrCat Example
-----------------------------------------------------------------------------
StrComp 比较两字串大小.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrComp(Str1, Str2 : PChar): Integer;
范例 uses SysUtils;
const
S1: PChar = 'Wacky';
S2: PChar = 'Code';
var
C: Integer;
Result: string;
begin
C := StrComp(S1, S2);
if C < 0 then Result := ' is less than ' else
if C > 0 then Result := ' is greater than ' else
Result := ' is equal to ';
Canvas.TextOut(10, 10, StrPas(S1) + Result +
StrPas(S2));
end;
Example
uses SysUtils;
procedure TForm1.Button1Click(Sender: TObject);

var
Msg: string;
CompResult: Integer;
begin
Msg := Edit1.Text;
CompResult := StrComp(PChar(Edit1.Text), PChar(Edit2.Text));
if CompResult < 0 then
Msg := Msg + ' is less than '
else if CompResult > 0 then
Msg := Msg + ' is greater than '
else
Msg := Msg + ' is equal to '
Msg := Msg + Edit2.Text;
ShowMessage(Msg);
end;

var
S1,S2: PChar;
I: Integer;
Res: string;
begin
S1:= 'Company';
S2:= 'COMPANY';
I:= StrComp(S1, S2);
if I>0 then Res:= '>' else
if I<0 then Res:= '<' else Res:= '=';
MessageDlg(S1+ Res+ S2, mtInformation, [mbOk], 0);
end;


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:49
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
StrCopy 拷贝字串.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrCopy(Dest, Source: PChar): PChar;
范例 uses SysUtils;
var
S: array[0..12] of Char;
begin
StrCopy(S, 'ObjectPascal');
Canvas.TextOut(10, 10, StrPas(S));
end;
Example
procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: PChar;
begin
GetMem(Buffer,Length(Label1.Caption) + Length(Edit1.Text) + 1);
StrCopy(Buffer, PChar(Label1.Caption));
StrCat(Buffer, PChar(Edit1.Text));
Label1.Caption := Buffer;
Edit1.Clear;
FreeMem(Buffer);
end;
## StrCopy, StrCat Example
-----------------------------------------------------------------------------
StrDispose 释放StrAlloc or StrNew所配置的空间.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 procedure StrDispose(Str: PChar);
范例 uses SysUtils;
const
S: PChar = 'Nevermore';
var
P: PChar;
begin
P := StrNew(S);
Canvas.TextOut(10, 10, StrPas(P));
StrDispose(P);
end;
-----------------------------------------------------------------------------
StrECopy 拷贝字串并传回字串结束位址.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrECopy(Dest, Source: PChar): PChar;
范例 uses SysUtils;
const
Turbo: PChar = 'Object';
Pascal: PChar = 'Pascal';
var
S: array[0..15] of Char;
begin
StrECopy(StrECopy(StrECopy(S, Turbo), ' '), Pascal);
Canvas.TextOut(10, 10, StrPas(S));
end;
Example
uses SysUtils;
const

Turbo: PChar = 'Object';
Pascal: PChar = 'Pascal';
var
S: array[0..15] of Char;
begin
StrECopy(StrECopy(StrECopy(S, Turbo), ' '), Pascal);
Canvas.TextOut(10, 10, string(S));
end;
-----------------------------------------------------------------------------
StrEnd 传回字串结束位址.
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrEnd(Str: PChar): PChar;
范例 uses SysUtils;
const
S: PChar = 'Yankee Doodle';
begin
Canvas.TextOut(5, 10, 'The string length of "' + StrPas(S)
+ '" is ' +IntToStr(StrEnd(S) - S));
end;
Example
procedure TForm1.Button1Click(Sender: TObject);

var
TextBuffer: PChar;
Ptr: PChar;
begin
GetMem(TextBuffer, Length(Edit1.Text)+1);
StrCopy(TextBuffer, PChar(Edit1.Text));
Ptr := StrEnd(TextBuffer);
Label1.Caption := ';
while Ptr >= TextBuffer do
begin
Ptr := Ptr ? 1;
Label1.Caption := Label1.Caption + Ptr^;
end;
FreeMem(TextBuffer);
end;

var
Str: PChar;
L: Word;
begin
...
L:= StrEnd(Str) - Str;
...
end;


情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:49
dzy
Rank: 2
等 级:新手上路
威 望:3
帖 子:708
专家分:0
注 册:2006-5-27
收藏
得分:0 
-----------------------------------------------------------------------------
StrIComp 比较两字串大小.(不分大小写)
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrIComp(Str1, Str2:PChar): Integer;
范例 uses SysUtils;
const
S1: PChar = 'Wacky';
S2: PChar = 'Code';
var
C: Integer;
Result: string;
begin
C := StrIComp(S1, S2);
if C < 0 then Result := ' is less than ' else
if C > 0 then Result := ' is greater than ' else
Result := ' is equal to ';
Canvas.TextOut(10, 10, StrPas(S1) + Result +
StrPas(S2));
end;
xample
uses SysUtils;
procedure TForm1.Button1Click(Sender: TObject);

var
Msg: string;
CompResult: Integer;
begin
Msg := Edit1.Text;
CompResult := StrIComp(PChar(Edit1.Text), PChar(Edit2.Text));
if CompResult < 0 then
Msg := Msg + ' is less than '
else if CompResult > 0 then
Msg := Msg + ' is greater than '
else
Msg := Msg + ' is equal to '
Msg := Msg + Edit2.Text;
ShowMessage(Msg);
end;

var
S1,S2: PChar;
I: Integer;
Res: string;
begin
S1:= 'ABC';
S2:= 'abc';
I:= StrIComp(S1, S2); { I := 0, ?.?. S1 = S2 }
if I>0 then Res:= '>' else
if I<0 then Res:= '<' else Res:= '=';
MessageDlg( S1 + Res + S2, mtInformation, [mbOk], 0);
end;
-----------------------------------------------------------------------------
StrLCat 字串相加.(指定长)
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrLCat(Dest, Source: PChar; MaxLen: Cardinal):
PChar;
范例 uses SysUtils;
var
S: array[0..13] of Char;
begin
StrLCopy(S, 'Object', SizeOf(S) - 1);
StrLCat(S, ' ', SizeOf(S) - 1);
StrLCat(S, 'Pascal', SizeOf(S) - 1);
Canvas.TextOut(10, 10, StrPas(S));
end;
Example
procedure TForm1.Button1Click(Sender: TObject);
var
FirstHalf: PChar;
SecondHalf: PChar;
HalfLen: Integer;
begin
HalfLen := StrLen(PChar(Edit1.Text)) div 2;
GetMem(FirstHalf,HalfLen+2);
GetMem(SecondHalf,HalfLen+2);
FirstHalf^ := Chr(0);
SecondHalf^ := Chr(0);
StrLCat(FirstHalf, PChar(Edit1.Text), HalfLen);
StrCat(SecondHalf, PChar(Edit1.Text) + HalfLen);
Application.MessageBox(FirstHalf, 'First Half', MB_OK);
Application.MessageBox(SecondHalf, 'Second Half', MB_OK);
FreeMem(FirstHalf);
FreeMem(SecondHalf);
end;

const
S1: PChar = '???';
S2: PChar = '?????????';
var
S: array[0..13] of Char;
begin
StrLCopy(S, S1, StrLen(S1));
StrLCat(S, S2, 6); { S :='??????' }
MessageDlg(S, mtInformation, [mbOk], 0);
end;
## StrLen, StrLCat Example
-----------------------------------------------------------------------------
StrLComp 比较两字串大小.(指定长)
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function StrLComp(Str1, Str2: PChar; MaxLen: Cardinal):
Integer;
范例 uses SysUtils;
const
S1: PChar = 'Enterprise'
S2: PChar = 'Enter'
var
Result: string;
begin
if StrLComp(S1, S2, 5) = 0 then
Result := 'equal'
else
Result := 'different';
Canvas.TextOut(10, 10, 'The first five characters are ' +
Result);
end;
example
uses SysUtils;
const
S1: PChar = 'Enterprise'
S2: PChar = 'Enter'
var
ComStr: string;
begin
if StrLComp(S1, S2, 5) = 0 then
ComStr := 'equal'
else
ComStr := 'different';
Canvas.TextOut(10, 10, 'The first five characters are ' + ComStr);
end;
const
S1: PChar = '?????????';
S2: PChar = '????????';
var
I: Integer;
S: string;
begin
I:= 5;
if StrLComp( S1, S2, I) = 0 then S:= '?????' else S:= '????????';
MessageDlg( '?????? '+ IntToStr(I)+ ' ???????? ????? '+ S, mtInformation,[mbOk], 0);
end;



情人太累,小姐太贵,友谊交往最实惠 ,没事开开“同学会”,拆散一对算一对!
2006-06-23 10:49
快速回复:[分享]新手和CN必看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.122268 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved