String, array manipulation function Copy Concat Delete Insert high midstr Pos SetLength strpcopy trimleft

Source: Internet
Author: User
Tags array length

The manipulation of strings and arrays is what every programmer has to master. Skilled use of these functions, in programming can be more handy.

1.Copy
Function Description: This function is used to copy characters from a specified range from a string. The function has 3 parameters. The first parameter is the data source (that is, the copied string), the second parameter is copied from one point in the string, and the third parameter is the length (or number) of the string to copy. The last function returns a new string (that is, the string content that we specify to copy).

Reference Example:

Var

s:string;

mystr:string; Save a new string

Begin

S: = ' I love china! ';

The following will get the "love" string in I love Chinese.

MyStr: = Copy (S, 3, 4);

End

Execution results, mystr equals "Love", "Love" string in "I Love china!" In the 3rd position, so the second argument is 3 and "Love" has 4 characters, so the third argument is 4.

2.Concat
Function Description: Concatenate two or more strings into a string.

Reference Example:

Var

S1, s2:string;

Begin

S1: = Concat (' A ', ' B '); Connect two strings, S1 variable equals AB.

S2: = Concat (' Borland ', ' Delphi ', ' 7.0 '); Connect three characters, S2 variable equals borland Delphi 7.0.

End

3.Delete
Feature Description: Deletes the string specified in the string. The function has three parameters. The first parameter is the string to be processed, the second parameter is where to start the deletion, and the third parameter is the number of characters deleted.

Reference Example:

Var

s:string;

Begin

S: = ' I like Reading CPCW. ';

The following code removes the "C" character from the s variable.

Delete (S, 16, 1);

End

At this point the s variable is I like Reading PCW. ("C" no longer exists).

4.High
Function Description: Returns the maximum value of the array subscript.

Reference Example:

Var

ARRTEXT:ARRAY[0..9] of Char;

I:integer;

Begin

I: = High (arrtext); The value of I is 9

End

5.Insert
Function Description: Insert a character (string). The function has three parameters. The first argument is the character (string) to be inserted, the second argument is the inserted string (the source string), and the third argument is where to insert it.

Reference Example:

Var

s:string;

Begin

S: = ' Wat is your name? ';

The words in the above sentence look for an "H" character, the following uses the Insert function to add H.

Insert (' h ', S, 2); Insert "H" from 2nd place.

End

6.LEFTSTR (unit: strutils)
Function Description: Returns the number of new characters (strings) specified to the left of the string. The function has two parameters. The first parameter is the complete string, and the second parameter is the specified number.

Reference Example:

Var

S, a:string;

Begin

S: = ' MSN Messenger ';

A: = Leftstr (S, 3); Starting at the far left, get the three characters on the left. So the A variable equals MSN.

End

7.Length
Function Description: This function is used to count the length of the specified string (that is, the number).

Reference Example:

Var

NLen1, Nlen2:integer; Used to hold string lengths

Begin

NLen1: = Length (' CPCW ');

NLen2: = Length (' Computer newspaper ');

End

Execution results, NLen1 equals 4,nlen2 equals 6. Since a Chinese character corresponds to a length of two characters, the length of the 3 kanji is 6.

8.Low
Function Description: Returns the minimum value of the array subscript.

Reference Example:

Var

ARRTEXT:ARRAY[1..9] of Char;

I:integer;

Begin

i:= High (arrtext); The value of I is 1

End

9.LowerCase
Function Description: Converts characters (strings) of English characters to lowercase.

Reference Example:

Var

S, a:string;

Begin

S: = ' ABC ';

A: = uppercase (S); After the uppercase function is converted, A is equal to ABC.

End

10.MIDSTR (unit: strutils)
Function Description: Returns the string within the specified range. The function has three parameters. The first argument is the source string, the second argument is the starting point, and the third argument is the end point. The second and third parameters specify the range of strings to be copied.

The copy function is similar to this function. MIDSTR is primarily used to process strings that contain Chinese characters.

Reference Example:

Var

s:string;

h:string;

Begin

S: = MIDSTR (' China ', 1, 2); s variable is ch

H: = Midstr (' Computer newspaper ', 1, 1); The h variable is "electricity". If you use the copy function, it should be h: = Copy (' Computer report, 1, 2), otherwise the returned will not be a "power" word. Therefore, it is best to use midstr when working with strings that contain Chinese.

End

11.Pos
Function Description: Find the location of the character (string). The function has two parameters. The first argument is the character (string) to look for, and the second argument is the searched character (string).

Reference Example:

Var

Npos:integer; Where to save the found characters

Begin

NPos: = Pos (' Like ', ' I like reading! ');

End

At this point NPOs equals 3. If it is not found, then NPOs is 0.

Note: The POS function is to distinguish the character size when it is found. If you want to implement a non-differentiated size, you need to use the uppercase or lowercase function to convert the characters (strings) of two arguments to "uppercase" or "lowercase" and then find them.

There is also a function to find the character (string)----ansipos, which is used exactly like the POS function. When you are looking for kanji, it is best to use the Ansipos function.

12.RIGHTSTR (unit: strutils)
Function Description: Returns the number of new characters (strings) specified to the right of the string. The function has two parameters. The first parameter is the complete string, and the second parameter is the specified number.

Reference Example:

Var

S, a:string;

Begin

S: = ' MSN Messenger ';

A: = Rightstr (S, 3); Starting from the far right, get the three characters to the right. So the A variable equals ger.

End

13.SetLength
Feature Description: Sets the string or dynamic array length. The function has two parameters. The first parameter is a string variable or a dynamic array variable to be set, the second parameter is the specified length, and its value range is from 0 to 255.

Reference Example:

Var

s:string;

Arrtext:array of Char; Defining a dynamic array

Begin

SetLength (S, 10); When set, the S variable can only be assigned a string with a value length of 10.

SetLength (Arrtext, 10); Dynamic arrays can be used only after allocating memory space for dynamic arrays using SetLength. The function of this code is equivalent to arrtext:array[0..9] of Char

End

14.StrPCopy
Function Description: Copies the string to the character array. The function has two parameters. The first parameter is the target array, and the second argument is string.

Reference Example:

Var

ARRCHAR:ARRAY[0..255] of Char; This declares an array of type char of length 256.

Begin

Strpcopy (Arrchar, ' Come on, baby! ');

End

15.Trim
Function Description: Delete the left and right spaces on both sides of the string (regardless of how many spaces are removed from the left and right sides).

Reference Example:

Var

s:string;

Begin

S: = ' Delphi 7.0 ';

S: = Trim (s);

End

16.TrimLeft
Function Description: Remove the space to the left of the string (regardless of how many spaces on the left are deleted).

Reference Example:

S: = Trimleft (' Delphi ');

17.TrimRight
Function Description: Remove the space to the left of the string (regardless of how many spaces on the left are deleted).

Reference Example:

S: = TrimRight (' Delphi ');

18.UpperCase
Function Description: Converts characters (strings) of English characters to uppercase.

Reference Example:

Var

S, a:string;

Begin

S: = ' abc ';

A: = uppercase (S); After the uppercase function is converted, A is equal to ABC.

End

Http://www.cnblogs.com/toosuo/archive/2007/12/17/1001414.html

String, array manipulation function Copy Concat Delete Insert high midstr Pos SetLength strpcopy trimleft

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.