These functions are included in strutils, so uses strutils is required;
Assume that the string is dstr: = 'delphi is the best', then
Leftstr (dstr, 5): = 'delph'
Midstr (dstr, 6, 7): = 'I is th'
Rightstr (dstr, 6): = 'e best'
~~~~~~~~~~~~~~~~~~~~~~~~~
Function rightstr
(Const STR: string; Size: Word): string;
Begin
If size> length (STR) Then size: = length (STR );
Rightstr: = copy (STR, length (STR)-size + 1, size)
End;
Function midstr
(Const STR: string; from, size: Word): string;
Begin
Midstr: = copy (STR, from, size)
End;
Function leftstr
(Const STR: string; Size: Word): string;
Begin
Leftstr: = copy (STR, 1, size)
End;
These functions are often used in combination with POS, length, and copy functions.
Function for splitting strings []
Delphi does not provide such a function, and finds
Function split (SRC, DEC: string): tstringlist;
VaR
I: integer;
STR: string;
Begin
Result: = tstringlist. Create;
Repeat
I: = pos (Dec, Src );
STR: = copy (SRC, 1, I-1 );
If (STR = '') and (I> 0) then
Begin
Delete (SRC, 1, length (DEC ));
Continue;
End;
If I> 0 then
Begin
Result. Add (STR );
Delete (SRC, 1, I + Length (DEC)-1 );
End;
Until I <= 0;
If SRC <> ''then
Result. Add (SRC );
End;
Procedure tform1.button1click (Sender: tobject );
VaR
SS: tstringlist;
STR, DEC: string;
Begin
Str: = '2017 | 1111 | 2222 | 3333 | ';
Dec: = '| ';
Ss: = split (str, dec );
Memo1.Lines. AddStrings (ss );
Ss. Free;
End;