For the tstringlist. Find function, I have something to say.

Source: Internet
Author: User
Tags first string

In Delphi, The tstringlist contains a function called find. It searches for a string in a stringlist and returns the index of this string in the stringlist. Descriptions in Delphi help are as follows:

Locates the index for a string in a sorted list and indicates whether a string with that value already exists in the list.

Delphi Syntax:

Function find (const S: string; var index: integer): Boolean; virtual;

C ++ Syntax:

Virtual bool _ fastcall find (const ansistring S, Int & Index );

Description

Use Find to obtain the index in a sorted list where the string s shocould be added. if the string S, or a string that differs from S only in case when casesensitive is false, already exists in the list, find returns true. if the list does not contain a string that matches S, find returns false. the index where s shocould go is returned in the index parameter. the value of index is zero-based, where the first string has the index 0, the second string has the Index 1, and so on.

Note: Only Use Find with sorted lists. for unsorted lists, use the indexof method instead.

 

As mentioned above, if this function is to be correctly executed, there is a premise that stringlist must be sorted first.

In this case, why not sort the function internally? In this case, this function will not be incorrectly used because you do not know the prerequisite.

Or you can change the function name more intuitively and easily, such as findsort.

 

Of course, if the stringlist is simply sorted and then searched, the integrity of the original data will be damaged. Therefore, if you put the sorting in the function, you can copy the stringlist before performing the operation.

The following is my pseudocode:

Strlsttmp: tstringlist;

Self. copyto (strlsttmp );

Strlsttmp. Sort ();

Strlsttmp. Find (strtofind); // strtofind: string to be searched

Index: = self. indexof (strtofind); // index: The index position of the stringlist in which the string is located.

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.