從字符串中提取單詞、從字符串中提取漢字的函數


//單元 
{
從字符串中提取單詞的函數} SysUtils.CharInSet();

procedure StrToWordList(str: string; var List: TStringList); var p: PChar; i: Integer; begin if List = nil then List := TStringList.Create; List.Clear; {去除重復} List.Sorted := True; List.Duplicates := dupIgnore; p := PChar(str); {把單詞以外的字符轉為空格, 並把大寫字母轉小寫} 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; {用空格分離單詞到列表} List.Delimiter := #32; List.DelimitedText := str; {單詞的開頭應該是字母, 去除其他} for i := List.Count - 1 downto 0 do begin if CharInSet(List[i][1], ['0'..'9', '-', '''']) then List.Delete(i); end; end; {從字符串中提取漢字的函數} procedure StrToHanZiList(str: string; var List: TStringList); var p: PWideChar; begin if List = nil then List := TStringList.Create; List.Clear; {去除重復} 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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM