Several string functions

Source: Internet
Author: User

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;

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.