Delphi字符串操作的常用函数一

来源:互联网 发布:全球云计算开源峰会 编辑:程序博客网 时间:2024/05/17 23:19

1.UpperCase

function UpperCase(const S: string): string;:将字符串转换为大写,其返回的string类型

[delphi] view plaincopy
  1. {按回车时字符串变成大写}  
  2. procedure TForm1.v_EditChrKeyPress(Sender: TObject; var Key: Char);  
  3. begin  
  4.    if Key = #13 then  
  5.    begin  
  6.       v_EditChr.Text := UpperCase(v_EditChr.Text);  
  7.    end;  
  8. end;  

 

2.LowerCase:将字符串转换为小写,其返回为string类型(其使用与UpperCase一致)

 

3.CompareStr(区分大小写

function CompareStr(const S1, S2: string): Integer; : 比较S1和S2的大小。如果S1<S2,则返回结果小于0;如果S1 = S2,则返回结果为0;如果S1>S2,则返回结果大于0;(S1和S2在比较时为有序类型,且其比较的值并不影响本地的变量)。

[delphi] view plaincopy
  1. {比较一个字符串是否为另一个字符串的子串}  
  2. procedure TForm1.BtnCompareStrClick(Sender: TObject);  
  3. var  
  4.  s1,s2,first1 : string;  
  5.  res,int : Integer;  
  6. begin  
  7.  res := 1;  
  8.  s1 := v_EditChr.Text;  
  9.  s2 := v_EditLow.Text;  
  10.  first1 := copy(s1,1,1);  
  11.  for int:=1 to Length(s1) do  
  12.  begin  
  13.    if Copy(s2,int,1) = first1 then  
  14.    begin  
  15.      s2 := Copy(s2,int,Length(s2));  
  16.      if CompareStr(s1,s2) = 0 then  
  17.      begin  
  18.         res := 0;  
  19.         break;  
  20.      end  
  21.      else  
  22.         res := 1;  
  23.    end;  
  24.  end;  
  25.  if res = 1 then  
  26.    showMessage('两个字符串不相同')  
  27.  else  
  28.    showMessage('两个字符串相同');  
  29. end;  

 

4. CompareText(不区分大小写

function CompareText(const S1, S2: string): Integer; :其两个字符串比较的结果也与CompareStr一致。

 

5. Concat

function Concat(s1 [, s2,..., sn]: string): string;:将两个或两个以上的字符串连接成一个字符串。

[delphi] view plaincopy
  1. procedure TForm1.btn_ConcatClick(Sender: TObject);  
  2. var  
  3.   BookName : string;  
  4. begin  
  5.   BookName:='Delphi程序设计';  
  6.   if Application.MessageBox(Pchar(Concat('确实需要删除名称为',BookName,'的商品图书信息吗?')),'提示',MB_YESNO) = IDYES then  
  7.   begin  
  8.     ShowMessage('操作成功!');  
  9.   end;  
  10. end;  


6. Copy

function Copy(S; Index, Count: Integer): string;
function Copy(S; Index, Count: Integer): array;
其中,S只能是一个字符串或者动态数组,数组元素不能为指针或对象;如果index大小超过S的长度,则返回一个空的字符串或数组;当S是一个动态数组时,你可以忽略index和count参数来复制整个数组。在CompareStr的例子中已经用到过Copy。这里不再重复举例;(Index表示作为截取字符串或元素的起始位置;Count表示截取的字符或数组元素的个数)

 

7. Delete

procedure Delete(var S: string; Index, Count:Integer);:从一个字符串中移除一个字符(从S[Index]开始,到第count字符并将其删掉[Delete removes a substring of Count characters from string S starting with S[Index]]),如果index的大小超过S的长度或者小于1,则没有字符被删掉;index表示删除字符串的起始位置;Count表示删除的字符数

[delphi] view plaincopy
  1. procedure TForm1.btn_DeleteClick(Sender: TObject);  
  2. var  
  3.   str1 : string;  
  4.   i : Integer;  
  5. begin  
  6.   str1 := v_EditChr.Text;  
  7.   for i:=1 to Length(v_EditChr.Text) do  
  8.   begin  
  9.     if Copy(str1,i,1) =  v_EditLow.Text then  
  10.       Delete(str1,i,1);  
  11.   end;  
  12.   v_EditChr.Text := str1;  
  13. end;  


 8. Insert

procedure Insert(Source: string; var S: string; Index: Integer);:[ Insert merges Source into S at the position S[index].]在S[Index]位置插入Source字符串。其中,S作为要插入的字符串;index作为插入字符串的起始位置。如果Index<1,则将Index指定为1;如果Index>Source的长度,则指定为Source最大的长度,并将S附加到Source后;如果Insert后组成的新字符串超过了内存,则报EOutOfMemory异常。

[delphi] view plaincopy
  1. procedure TForm1.btn_InsertClick(Sender: TObject);  
  2. var  
  3.   temp : string;  
  4. begin  
  5.   temp := v_EditChr.Text;  
  6.   if Copy(v_EditChr.Text,1,2) <> 'AB' then  
  7.     Insert('AB',temp,2);  //在temp的第2个索引后插入AB  
  8.     v_EditChr.Text:=temp;  
  9. end;  


 

0 0
原创粉丝点击