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