2014年1月23日 星期四

字串基本處理 (參考書上所寫)

1CompareStr 比較兩字串的大小(大小寫視為不同),傳回第一個不相同字元 ASCII 碼的差,若字串完全相同,則傳回 0 (字串的編號從 1 開始)。 function CompareStr(const S1, S2 : string) : Integer;
傳回結果:
當 S1>S2 傳回大於 0 的整數
當 S1<S2 傳回小於 0 的整數
當 S1=S2 傳回 0
範例:
S1:='Delphi 6.0';
S2:='delphi 6.0';
if CompareStr(S1,S2)<>0 then
showMessage('大小寫被視為不同的!');
2CompareText比較兩字串的大小(大小寫視為相同),傳回第一個不相同字元 ASCII 碼的差function CompareText(const S1, S2:string) : Integer;
傳回結果:
當 S1>S2 傳回大於 0 的整數
當 S1<S2 傳回小於 0 的整數
當 S1=S2 傳回 0
範例:
S1:='Delphi';
S2:='delphi';
ResultFlag:=CompareText(S1,S2);
if ResultFlag <>
ShowMessage('Delphi <>
else if ResultFlag > 0 then
ShowMessage('Delphi > delphi')
else
ShowMessage('Delphi = delphi');
{Delphi = delphi}
3Length傳回字串 S 的長度function Length(S) : Integer;
範例:
S:='Delphi 6.0 讚 '; //中文算兩個Bytes,長度包含空白
ShowMessage(IntToStr(Length(S)));
{14}
4Concat傳回字串相加的結果。使用字串相加(+)運算子,亦有相同的效果,且執行速度較快function Concat(s1 [, s2, ..., sn ] : string) : string;
範例:
S1:='今天';
S2:='下雨';
ShowMessage(Concat(S1, S2));
{今天下雨}
5Insert將 Source 字串插入 S 字串的第 count 個字元位置(字串的編號從 1 開始)procedure Insert(Source : string; var S : string; Index : Integer);
範例:
S1:='I Love you!';
S2:=' don''t';
insert(S2, S1, 2);
showmessage(S1);
{I don't Love you!}
6Copy傳回 S 字串的第 Index 字元起,拷貝 Count 個字元(字串的編號從 1 開始)function Copy(S; Index, Count:Integer) : string;
範例:
S:='Delphi 2006 is good??!';
S:=Copy(S, 1, 11);
ShowMessage(S);
{Delphi 2006}
7Delete將 S 字串從第 Index 字元開始,刪除 Count 個字元(字串的編號從 1 開始)procedure Delete(var S : string; Index, Count : Integer);
範例:
S:='Delphi is good??!';
Delete(S, 15, 2);
ShowMessage(S);
{Delphi is good!}
8Pos傳回 Substr 字串於 S 字串的開始位置(字串的編號從 1 開始)function Pos(Substr : string; S : string) : Integer;
範例:
S:=' Delphi is good ';
while Pos(' ', S)>0 do
begin
i:=Pos(' ', S);
showmessage(inttostr(i));
S[i]:='_';
end;
{1, 8, 11, 16, 17}
9Trim清除字串前後的空白function Trim(const S : string) : string;
範例:
S1:=' Delphi 6.0 ';
ShowMessage(Trim(S1));
{'Delphi 6.0'}
10TrimLeft清除字串左邊的空白function TrimLeft(const S : string) : string;
範例:
S1:=' Delphi 6.0 ';
ShowMessage(TrimLeft(S1));
{'Delphi 6.0 '}
11TrimRight 清除字串右邊的空白function TrimRight(const S : string) : string;
範例:
S1:=' Delphi 6.0 ';
ShowMessage(TrimRight(S1));

沒有留言:

張貼留言