Basic string operations

Source: Internet
Author: User
Tags control characters printable characters stringreplace

In string Analysis in Delphi, we have analyzed the basic string types. Next we will talk about string operations.

In actual programming, these operations are often used. You don't have to worry about remembering them all at first, as long as you know that these functions are available, you can find them when necessary.

1. Use the + operator to connect strings
VaR X: integer; S1: string; S2: string; begin S1: = 'hello'; S2: = 'World'; showmessage (S1 + ''+ S2 ); {connected to three strings S1, S2, and space string} X: = 2011; showmessage ('this year is '+ inttostr (x )); {inttostr function converts integer to string type} end;

The displayed message boxes are as follows:

2. Script operator []

The script operator can extract individual characters from a string.

VaR S1: string; S2: string; begin S1: = 'Hello world'; S2: = S1 [1]; showmessage (S2 ); {get the first character of the S1 string h} end;

The string starts from 1. The first character in the string is in S [1]. The 0 element (s [0]) of the short string contains the length of the string, instead of the first character of the string, X [0] cannot be accessed in long strings (ansistring) and wide strings (widestring).

3. control characters in strings

String constants (string literal or String constant) are generally caused by two single quotes. If the string itself also has single quotes, two single quotes are required, as shown below:

'BORLAND'           { BORLAND }'You''ll see'       { You'll see }''''                { ' }''                  { null string }' '                 { a space }

Control characters can be embedded in strings. This function is very useful when some non-printable characters need to be added to strings. The control characters embedded in the string are used to send to the serial device.

Use character # To add control characters to the string, for example:

VaR S1: string; S2: string; begin S1 :#89 #111 #117; S2: = 'you '; {In fact, S1 is the same as S2} showmessage (S1 + #13 #10 + S2); {#13 #10 indicates line feed and Carriage Return carriage-return-line-feed} end;

The following figure shows the effect after running:

4. Extend the string with multiple lines of code

A string enclosed in single quotes is a String constant (string literal or String constant). It can contain a maximum of 255 characters.

To enhance readability and maintainability, it is necessary to divide a string into multiple rows and use the + operator, for example:

  ShowMessage('This is very, very long message ' +    'that seems to go on and on forever. In order ' +    'to make the code more readable the message has ' +    'been split across several lines of code.');
5. String comparison

String comparison operators include: = (equal to), <> (unequal), <(less than),> (greater than), <= (less than or equal) ,> = (greater than or equal ).

These operators compare strings Based on ASCII values. In most cases, you only need to use the equal sign to determine whether the string is equal to or not equal to a value. For example:

VaR S1: string; begin S1: = 'Hello world'; If S1 = 'Hello world' then showmessage ('equal ') {an equal message box will be displayed} else showmessage ('unequal '); end;

The running result is as follows:

6. String operation functions

Basic commonly used string operation functions are defined in the system or sysutils unit. Let's use an example to explain the usage of commonly used string functions.

Const S1: prop string = 'Hello world'; var X: integer; SP: pchar; S2: string; RS: string; {display final result} begin SP: = 'pchar '; // The copy function starts from the last character. There is a total of characters S2: = copy (S1, length (S1), 1 ); {S2 = 'D'} RS: = Rs + S2 + #13 #10; // Delete the delete function S2: = S1; Delete (S2, 1, 1); {S2 = 'Ello world'} RS: = Rs + S2 + #13 #10; // Format String function S2: = format ('Time is: % d: % d ', [12, 15, 40]); {S2 = 'Time is: 12:15:40'} {% d indicates that an integer will be placed here, [2011] indicates placing the value 2011} RS: = Rs + S2 + #13 #10; // converts the value to the string inttostr Function x: = 65535; S2: = inttostr (x); {S2 = '10'} RS: = Rs + S2 + #13 #10; // returns the string length Function x: = ord (S1 [0]); {x = 11} X: = length (S1); {because S1 is 'string' type, so we can use S1 [0] To get the character length} RS: = Rs + inttostr (x) + #13 #10; // convert it to lower case lowercase function S2: = lowercase (S1); {S2 = 'Hello world'} RS: = Rs + S2 + #13 #10; // convert to uppercase function S2: = uppercase (S1); {S2 = 'Hello world'} RS: = Rs + S2 + #13 #10; // returns the string S2 with the specified repeating characters: = stringofchar ('A', 10); {S2 = 'aaaaaaaaaaa'} RS: = Rs + S2 + #13 #10; // strpas converts an empty ending string (pchar or character array) into a Pascal string S2: = strpas (SP); {S2 = 'pchar '} RS: = Rs + S2 + #13 #10; // strpcopy converts a Pascal string to an empty ending string strpcopy (SP, 'World hello '); {sp = 'World hello'} RS: = Rs + strpas (SP) + #13 #10; // trim is used to remove spaces before and after the string and control character S2: = #13 #10 'Hello world'; S2: = trim (S2); {S2 = 'Hello world'} RS: = Rs + S2 + #13 #10; // stringreplace is used to replace the string with the character S2: = 'abbcc '; S2: = stringreplace (S2, 'A', 'E', [rfreplaceall]); RS: = Rs + S2 + #13 #10; showmessage (RS); end;

The running result is as follows:

The above code is successfully tested in Delphi7.

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.