function GetValueTrat(aValue: string): string;const number = [0 .. 9];const letter = ['a' .. 'z','A' .. 'Z'];var i: Integer;begin for i := 1 to length(aValue) do begin if (not(StrToInt(aValue[i]) in number)) or (not(aValue[i] in letter)) then raise Exception.Create('Non valIDo'); end; Result := aValue.Trim;end;
但是,例如,如果aValue =’Hello’,StrToInt函数会引发异常.
解决方法 一组独特的Char可用于您的目的.function GetValueTrat(const aValue: string): string;const CHARS = ['0'..'9','a'..'z','A'..'Z'];var i: Integer;begin Result := aValue.Trim; for i := 1 to Length(Result) do begin if not (Result[i] in CHARS) then raise Exception.Create('Non valIDo'); end;end;
请注意,在函数中,如果aValue包含空格字符 – 例如“测试值” – 会引发异常,因此在if语句之后使用Trim是无用的.
像^ [0-9a-zA-Z]这样的正则表达式可以在我看来以更优雅的方式解决您的问题.
编辑
根据@RBA’s comment的问题,System.Character.TCharHelper.IsLetterOrDigit可以用来代替上述逻辑:
if not Result[i].IsLetterOrDigit then raise Exception.Create('Non valIDo');总结
以上是内存溢出为你收集整理的数组 – Delphi检查字符是否在’A’范围内.’Z’和’0′..’9′全部内容,希望文章能够帮你解决数组 – Delphi检查字符是否在’A’范围内.’Z’和’0′..’9′所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)