Create a REPLACE function of strings in delphi

zhaozj2021-02-16  50

Create a REPLACE function of strings in delphi

Editor Note: In fact, Delphi's StringReplace function is designed to meet this needs. But we also published this article - at least the author provides an alternative, exploring the spirit, should encourage!

This is the first article here, and the level is not very high, just discuss a foundation but very practical, I hope to give you some help.

Friends who have used VB or ASP know that there is a very practical replace function in VB, and its function is to replace a string with a string (also called a mode string) into the specified string. For example, there is such a string: s: = 'apple is apple!', After the REPLACE function Replace (s, 'apple', 'box'), S is changed to 'Box is Box!'. The length of the string also changes accordingly. This is a very useful function. Everyone knows that the structured query statement SQL is often used when developing a database system, and this statement is more sensitive to some characters, such as single quotes, if there is a single in the SQL statement Quotation marks (because single quotes are a meaningful character specified in SQL) programs will have unexpected errors, and can even be used by others to generate serious security vulnerabilities (this is the famous SQL injection attack, I believe everyone still I remember this vulnerability that I have found before the 9CBS Forum). At this time, you need to replace single quotes to other strings or empty strings when you read the data, and then replace it back so that you can record single quotes in this data record without errors.

However, I didn't find similar functions in Delphi (maybe I didn't find it?), It is very inconvenient, so I wrote one, which is more convenient in future database system development. Said so much nonsense, the following is the code, plus comments should be easier to understand.

Procedure Replace (var s: string; const sourcechar: pchar; const rchar: pchar);

// The first parameter is the original string, the second is the mode string, the third is the replacement string

VAR

TA, I, J: Integer;

M, N, PN, Sn: integer

Slen, Sclen, Rclen: Integer; // Slen represents the length of the original string, the sclen represents the length of the mode transmission, and RCLEN means the length of the replacement string.

Issame: integer;

Newp: array of char; // Used to save the replacement character array

Begin

Slen: = Strlen (PCHAR (S)); Sclen: = Strlen (SourceCha); RCLEN: = Strlen (rchar);

J: = POS (String (SourceChar), S);

S: = S CHR (0); TA: = 0; i: = j;

While s [i] <> chr (0) Do // This time the time of TA statistical mode string appears in the original string

Begin

N: = 0; Issame: = 1;

For m: = I to i sclen-1 do

Begin

IF m> Slen dam Issame: = 0; Break; end;

IF S [M] <> Sourcechar [N] the beginning ;. = 0;

N: = N 1;

END;

If ISSAME = 1 THEN BEGIN TA: = Ta 1; I: = M; ELSE I: = I 1;

END;

IF j> 0 thenbegin

PN: = 0; SN: = 1;

SETLENGTH (NewP, Slen-TA * Sclen Ta * RCLEN 1); // Assign the length of NewP, 1 indicates a # 0 end of the end

While s [SN] <> chr (0) do // main loop, start replacement

Begin

N: = 0; Issame: = 1;

For m: = Sn to Sn Sclen-1 Do // Compare Subtrings and Mode Strings

Begin

IF m> Slen dam Issame: = 0; Break; end;

IF S [M] <> Sourcechar [N] the beginning ;. = 0;

N: = N 1;

END;

If ISSAME = 1 THEN / /

Begin

For m: = 0 TO RCLEN-1 DO

Begin

NEWP [PN]: = rchar [M]; PN: = PN 1;

END;

SN: = SN Sclen;

end

Else

Begin // different

NEWP [PN]: = s [SN];

PN: = PN 1; SN: = SN 1;

END;

END;

NEWP [PN]: = # 0;

S: = String (newp); // Reset S, replace it!

END;

END;

In fact, this is a basic data structure problem, and today's practice data structure is now dragging and dropping the control. Of course, this function is not optimal. I tested the string that replaced 10,000 words. The time of half a second, the time complexity is still relatively high, and if you have better ways, welcome to discuss!

转载请注明原文地址:https://www.9cbs.com/read-27356.html

New Post(0)