The function of extracting words from strings and extracting Chinese characters from strings in Delphi

Source: Internet
Author: User

{function to extract Word from string}
Procedure Strtowordlist (str:string; var list:tstringlist); Var
P:pchar;
I:integer;
Begin
If list = Nil then list: = Tstringlist.create;
List.clear;
{Remove Duplicates}
list.sorted: = True;
List.duplicates: = Dupignore;
 
P: = Pchar (str);

{Convert a character other than a word to a space and turn the capital letter to lowercase}
While p^ <> #0 do
Begin
Case p^ of
' A ' ... ' Z ': p^: = Chr (Ord (p^) + 32);
' A ' ... ' Z ', ' 0 ' ... ' 9 ', ', ', '-':;
else p^: = #32;
End
Inc. (P);
End

{Separate words to list with spaces}
List.delimiter: = #32;
List.delimitedtext: = str;

{The beginning of a word should be a letter, remove other}
For I: = list.count-1 downto 0 Do
Begin
If Charinset (list[i][1], [' 0 ' ... ' 9 ', '-', ' '] then
List.delete (i);
End
End

{function to extract Chinese characters from string}
Procedure Strtohanzilist (str:string; var list:tstringlist);
Var
P:pwidechar;
Begin
If list = Nil then list: = Tstringlist.create;
List.clear;
{Remove Duplicates}
list.sorted: = True;
List.duplicates: = Dupignore;
 
P: = Pwidechar (str);
While p^ <> #0 do
Begin
Case p^ of
#$4e00.. #$9fa5:list.add (p^);
End
Inc. (P);
End
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.