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

1.UpperCase

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

{按回车时字符串变成大写}
procedure TForm1.v_EditChrKeyPress(Sender: TObject; var Key: Char);
begin
   if Key = #13 then
   begin
      v_EditChr.Text := UpperCase(v_EditChr.Text);
   end;
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在比较时为有序类型,且其比较的值并不影响本地的变量)。

{比较一个字符串是否为另一个字符串的子串}
procedure TForm1.BtnCompareStrClick(Sender: TObject);
var
 s1,s2,first1 : string;
 res,int : Integer;
begin
 res := 1;
 s1 := v_EditChr.Text;
 s2 := v_EditLow.Text;
 first1 := copy(s1,1,1);
 for int:=1 to Length(s1) do
 begin
   if Copy(s2,int,1) = first1 then
   begin
     s2 := Copy(s2,int,Length(s2));
     if CompareStr(s1,s2) = 0 then
     begin
        res := 0;
        break;
     end
     else
        res := 1;
   end;
 end;
 if res = 1 then
   showMessage('两个字符串不相同')
 else
   showMessage('两个字符串相同');
end;

 

4. CompareText(不区分大小写

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

 

5. Concat

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

procedure TForm1.btn_ConcatClick(Sender: TObject);
var
  BookName : string;
begin
  BookName:='Delphi程序设计';
  if Application.MessageBox(Pchar(Concat('确实需要删除名称为',BookName,'的商品图书信息吗?')),'提示',MB_YESNO) = IDYES then
  begin
    ShowMessage('操作成功!');
  end;
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表示删除的字符数

procedure TForm1.btn_DeleteClick(Sender: TObject);
var
  str1 : string;
  i : Integer;
begin
  str1 := v_EditChr.Text;
  for i:=1 to Length(v_EditChr.Text) do
  begin
    if Copy(str1,i,1) =  v_EditLow.Text then
      Delete(str1,i,1);
  end;
  v_EditChr.Text := str1;
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异常。

procedure TForm1.btn_InsertClick(Sender: TObject);
var
  temp : string;
begin
  temp := v_EditChr.Text;
  if Copy(v_EditChr.Text,1,2) <> 'AB' then
    Insert('AB',temp,2);  //在temp的第2个索引后插入AB
    v_EditChr.Text:=temp;
end;


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值