Delphi determines whether the string contains Chinese characters and returns the position of the kanji

Source: Internet
Author: User

1, Function code
{

Determines whether a string contains Chinese characters
JUDGESTR: The string to be judged
Posint: Position of first kanji
}
function Tform2.ishavechinese (judgestr:string; var posint:integer): boolean;
Var
P:pwidechar; The character to be judged
Count:integer; Include Kanji location
Ishave:boolean; Whether to include Chinese character return value
Begin

Ishave: = false; Whether to include Chinese character return value defaults to False
Count: = 1; Include kanji location default to 1

P: = Pwidechar (JUDGESTR); Convert the string to be judged

Loop to judge each character
While p^ <> #0 do
Begin
Case p^ of
#$4e00. #$9fa5:
Begin
Ishave: = true; Sets whether to include a Chinese character return value of True
Posint: = Count; Set include Chinese character position
Break Exit loop
End

End

INC (P);
INC (count); Include the position increment of Chinese characters
End

Result: = Ishave;
End


2, Example:

Procedure Tform2.button3click (Sender:tobject);
Var
TESTSTR1, teststr2:string;
Posint:integer;
Begin
TESTSTR1: = ' 12345 ';
TESTSTR2: = ' 123 Kanji 45 ';

If self. Ishavechinese (TESTSTR1, posint) = True Then
Begin
ShowMessage (testStr1 + ' contains kanji: ' + inttostr (posint));
End
Else
Begin
ShowMessage (testStr1 + ' does not contain Chinese characters ');
End

If self. Ishavechinese (TESTSTR2, posint) = True Then
Begin
ShowMessage (testStr2 + ' contains kanji: ' + inttostr (posint));
End
Else
Begin
ShowMessage (testStr2 + ' does not contain Chinese characters ');
End
End

Source:http://blog.163.com/[email protected]/blog/static/6723961920147237131189/

Delphi determines whether the string contains Chinese characters and returns the position of the kanji

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.