Use the string replacement function StringReplace to delete the specified character or string in the string.

Source: Internet
Author: User

The title is a little long ~

The StringReplace function can be used to delete the specified characters (strings) in a string. The following is a small example:

 

Delete the 'bad' string from the string:

1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, Buttons;
8
9 type
10 TForm1 = class (TForm)
11 btn1: TBitBtn;
12 edt1: TEdit;
13 procedure btn1Click (Sender: TObject );
14 private
15 {Private declarations}
16 public
17 {Public declarations}
18 end;
19
20 var
21 Form1: TForm1;
22
23 implementation
24
25 {$ R *. dfm}
26
27 procedure TForm1.btn1Click (Sender: TObject );
28 var
29 original: string;
30 AfterDelete: string;
31 begin
32 original: = edt1.Text;
33 AfterDelete: = StringReplace (original, 'bad', '', [rfReplaceAll]);
34 ShowMessage (AfterDelete );
35 end;
36
37 end.
38

 

 

Function Description:

 

 

From SysUtils. pas

function StringReplace(const S: string;const OldPattern: string;const NewPattern: string;Flags: TReplaceFlags): string; overload;

Unit: SysUtils

Type:Function

Visibility:Public

 

Description

Replaces occurrences of a substring within a string.

StringReplace replaces occurrences of the substring specified by OldPattern with the substring specified by NewPattern in the string S.

Flags is a SysUtils. TReplaceFlags type parameter. if rfIgnoreCase is set, the replacement is case-sensitive; otherwise case is ignored. if rfReplaceAll is on, all occurrences of OldPattern are replaced; if not, only the first occurrence is replaced.

Note:The parameters S, OldPattern, NewPattern, and the return value are of type UnicodeString. to do the replacement in an AnsiString context, use the StringReplace function. also, to do the replacement in a WideString context, use the WideStringReplace function.

Note:Recursive replacement of substrings is not supported. This means that if the substitution of OldPattern results in a new match for NewPattern, that match is not replaced.

To replace all occurrences of the substring within the string, you may also use the ReplaceStr function to do a case-sensitive search, or ReplaceText to do a case-insensitive search.

 

 

{☆Recommended ☆: rebate network, online shopping for rebates, save money and new portal}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.