Delphi 10.3 使用if多條件例如 if 'a' in ['a', 'b'] 時報錯,提示operator not applicable to this operand type;
想將字串轉A碼使用
ord(tmp) //tmp:string='a'
時也報錯,提示incompatible type
但是直接ord('a') 確沒有問題
請教各位大神這是什么原因?看了網上的代碼跟我的一模一樣的但卻沒有問題?
uj5u.com熱心網友回復:
關于ord的問題已經通過https://blog.csdn.net/xufeichen/article/details/4480697的方法解決了,但是if的問題還沒有uj5u.com熱心網友回復:
10.2是可以的,但是便宜有一個警告,這樣if AnsiChar('a') in ['a', 'b'] then ...
就好了
或者可以使用if CharInSet('a', ['a', 'b']) then ...
uj5u.com熱心網友回復:
但是編譯有一個警告uj5u.com熱心網友回復:
感謝樓上的回答,都不行,很尷尬,難道是版本的問題?我以前用10.1也是這樣,每次都只能用N多個and連接uj5u.com熱心網友回復:
從來沒有聽說過CharInSet('a', ['a', 'b']) 不行..................uj5u.com熱心網友回復:
換到KeyPress事件試一試:procedure TFormMain.DBEditEh4KeyPress(Sender: TObject; var Key: Char);
var
i: Integer;
begin
if DMADO.ADO考試.IsEmpty then
Exit;
if (AnsiChar(Key) in [#13, Char(VK_DOWN)]) and (ActiveControl is TDBEditEh) then
begin
Key := #0;
uj5u.com熱心網友回復:
小猴子用的是10.3吧,不知道樓主為什么不行~~~uj5u.com熱心網友回復:
這樣也可以if not(CharInSet(Key, ['0' .. '9', '.', '-', #8])) then
Key := #0;
if (Key = '.') and (Pos('.', Edit4.text) > 0) then
Key := #0;
uj5u.com熱心網友回復:
不科學,D7和D10都可以測驗通過,樓主的完整代碼是什么?var
tmp: string;
begin
tmp := 'a';
if tmp[1] in ['a', 'b'] then
begin
showmessage('matched');
end;
uj5u.com熱心網友回復:
CharInSet是高版本Delphi提倡的,in的話char常量有WideChar和AnsiChar的歧義uj5u.com熱心網友回復:
CharInSet內部也是用了in,只是避免了直接使用字符字面值出現的編譯器警告uj5u.com熱心網友回復:
都是直接用pos函式來判斷a字串是否在b字串的,還真沒用到過樓主那種方法轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/19220.html
標籤:語言基礎/算法/系統設計
上一篇:Delphi代碼語法錯誤怎么改
下一篇:求指教
