Original from: "100 script net http://www.pc100.net/"
the basic functions of a few string processing
a) Substring positioning--Gets the position of the substring in the original string (not the array subscript, but the first few)
1 function pos (' substring ', ' original string '): integer
Note: When there is Chinese, it is best to use this: POS (' substring ', widestring (' original string ')), which avoids the problem when the second half of the previous character and the first half of the next Chinese character also happen to form a Chinese character.
b) Substring interception--copies the partial substring in the original string (index is not an array subscript, but the first)
1 function Copy (S; Index, Count:integer): string
Description: In fact, the function can also copy the character array, except that "source data" and "return data" are all character arrays
function Copy (S; Index, Count:integer): Array
Additional: Several more targeted interception operations
1) Intercept source string The left end of a certain length: Strutils.pas
function Leftstr (Const str:string; Size:word): string;
2) intercept the source string in the middle of a certain length of bits: Strutils.pas
function Midstr (Const str:string; From,size:word): string;
3) intercept the source string at the right end of a certain length: Strutils.pas
function Rightstr (Const str:string; Size:word): string;
c) String segmentation--splitting the original string into several substrings using a delimiter
1 strlist:=tstringlist.create;
2 strlist.delimiter:= ' | ';
3 strlist.delimitedtext:= ' to | division | | | |
Description: After this operation, each element of the split is stored in strlist
d) Length calculation--Calculating the length of a string
function Length (S): Integer;
e) Length Setting--The length of the human-specified string
Procedure SetLength (Var S; Newlength:integer);
When the string is displayed, the displayed "character length" is determined based on the length value set instead of the Terminator #0.
f) String padding
Var
sourcestring,resultstring:string;
Begin
sourcestring:= ' ABCD ';
Resultstring 8-bit, less high-level with ' 0 ' padding
Resultstring:=stringofchar (' 0 ', 8-length (sourcestring)) +sourcestring;
ShowMessage (resultstring);//The end result is ' 0000ABCD '
End
Description: A string with insufficient number of bits, populated with a specific character at a high position
g) Add a delimiter (custom function) to the string
function Delimiterinsert (Const s:string; Const scut:string): String; var i:integer;begin i:= 1; While I < length (s) does begin If i = 1 then result: = Copy (S, I, 2) else Result: = result + scut+ Co PY (S, I, 2); I: = i + 2; End;end;
Test:
var sourcestr,destinatestr:string;begin sourcestr:= ' abcdefgh '; DESTINATESTR: = Delimiterinsert (Sourcestr, '-');//each two characters plus "-" showmessage (DESTINATESTR); end;
Run results (slightly):
h) Single quotation mark
aa: = ' aaa '; then AA results in AAA '
is actually two single quotes to denote a single quote bb:= ' BB ' BB '; BB's result for BB ' BB
i) ASCII code converted into characters
var ss:string;begin Ss:=char (+) +char ($); ShowMessage (ss); end;
Operation Result:
J ) Character Substitution
var ss:string;begin ss:= ' AAABBBCCC '; Ss:=stringreplace (ss, ' B ', ' m ', [Rfreplaceall]); ShowMessage (ss); end;
Operation Result:
Note: a more comprehensive string-handling function, which can be consulted:
A) http://ideasforjava.javaeye.com/blog/850360
b) Delphi Help documentation, Help->delphi assist under Strutils
Two, String,pchar, the mutual conversion between the character array
A) The allocation of string types in memory
Description: 1) Terminator #0 actually just to be compatible with the Pchar type, the actual output string is based on the value stored in "string length" instead of the Terminator #0, the code is as follows:
Var
srcstring:string;
Begin
srcstring:= ' 12345 ';//At this time the length is 5
ShowMessage (' Raw string: ' +srcstring);
srcstring:=srcstring+ ' addstr '; At this point the string is "12345AddStr" and the length is automatically modified to 11
ShowMessage (' appended string: ' +srcstring '); Display as: "12345AddStr"
SetLength (srcstring,3); The string length is artificially set to 3, and the actual length should be 11.
ShowMessage (' Man-set length after string: ' +srcstring ');//based on stored length values, instead of Terminator #0, final output ' 123 '
End
2) The role of the reference count is that when the number of pointers to this heap space is zero, the compiler automatically frees heap memory without requiring
To manually release
3) Use the copy on write mechanism to increase the utilization of memory space
Attached: What is the copy on write mechanism?
Multiple references point to the same piece of memory address. When one of the references has a write operation, the copy is re-copied to the other memory while
The original reference count is reduced by 1. This is done to increase the utilization of memory.
BUG: When a string type is a parameter, even though the first "temporary reference" and "original reference" point to the same piece of memory in the heap,
When you write to a block of memory that the temporary reference refers to in the body of the function that is called, the memory block is actually
The content is copied and combined with specific operations to generate the string that the caller needs and deposit into a newly opened heap of memory,
The contents of the block of memory that the original reference points to are not changed, which is why the string type cannot be used
The return value of the PASS function as an output parameter (but can be used as an input parameter) instead of using the Pchar type as the output parameter
Transfer function return value
called as a function of the "output parameter" as a String type:
Procedure Tform1.stringtypeasoutputparam (sourcestr:string);
Begin
sourcestr:=sourcestr+ ' bbbbb ';
ShowMessage (' Modified string: ' +sourcestr ');//Display as: AAAAABBBBBB
End
Call this function:
Var
mysourcestr:string;
Begin
mysourcestr:= ' AAAAA ';
ShowMessage (' Raw string: ' +mysourcestr); Display as: ' AAAAA '
Stringtypeasoutputparam (MYSOURCESTR); Passing the string type as an output parameter
ShowMessage (the string after the function is called: ' +mysourcestr);//Still shown as: ' AAAAA '
End
b) String and Pchar
Compatible with the two, can be directly "type strong turn"
Var
mystring:string;
Mypchar:pchar;
Begin
mystring:= ' ABCDEFG ';
ShowMessage (' The string before conversion is: ' +mystring); At this point the string is ' ABCDEF '
Mypchar:=pchar (myString);
Mystring:=string (Mypchar);
ShowMessage (' consecutive converted strings are: ' +mystring '); At this point the string is still ' ABCDEF '
End
c) Character array and Pchar
1) Character array subscript starting from 0
2) character array subscript starting from 1
Var
MYARRAYCHAR:ARRAY[1..10] of Char;
Mypchar:pchar;
Begin
Mypchar:=pchar (' ABCDEF ');
Strcopy (@myArrayChar, Mypchar); Strpcopy can also be used (@myArrayChar, ' ABCDEF ');
ShowMessage (myarraychar[1]); The output value is ' A ' NOTE: This can not be compiled if it is accessed with myarraychar[0]
myarraychar[1]:= ' 1 ';
Mypchar:=pchar (@myArrayChar);
ShowMessage (String (Mypchar)); The entire string is ' 1BCDEF '
End
d) string and character array: need to transition with Pchar type
Var
mystring:string;
MYARRAYCHAR:ARRAY[1..10] of Char;
Begin
mystring:= ' ABCDEF ';
Strcopy (@myArraychar, PChar (myString));//may also use Strpcopy (@myArraychar, myString);
myarraychar[1]:= ' 1 ';
Mystring:=string (PChar (@myArrayChar));
ShowMessage (myString); The converted value is ' 1BCDEF '
End
Note: The difference between strcopy and strpcopy
parameter type: Strcopy (pchar type, pchar type);
Strpcopy (pchar type, string type);
e) Recommended Usage:
1) When a string is copied
Strpcopy (pchar type, string type);
2) When you need to use a string as an "output parameter"
Using the Pchar type
3) When defining a character array
CHARARRAY:ARRAY[0..N] of char;//subscript as much as possible starting from 0, rather than starting from 1
f) String and widestring
String:ansi Character Set
Widestring:unicode Character Set
Processing of strings in "Go" Delphi