Crystal Reports Check If String Contains Any Numerical Value

回眸只為那壹抹淺笑 提交于 2019-12-10 22:26:48

问题


I'm using Crystal Reports and in 1 of my formulas, I'd like to check if a string contains any numerical value or not. Examples are shown below...

"Chris(12)" Returns True
"123"       Returns True
"Pot"       Returns False
"John0"     Returns True

I've already achieved what I want using the INSTR() function. I did it like this...

if INSTR(string,"0") <> 0 or INSTR(string,"1") <> 0 or INSTR(string,"2") <> 0 ... then
   True
else
   False

I'd just like to know if there's any shorter or more efficient code. Thank you very much.


回答1:


Create a custom function named ContainsNumber:

Function (Stringvar text)

  Local Booleanvar found := False;
  Local Numbervar i;

  For i := 1 To Len(text) Do (

    If IsNumeric(Mid(text, i, 1)) Then (
      found := True;
      Exit For
    )

  );

  found;

Use in formula field:

// FALSE
ContainsNumber ("ABC")

// TRUE
ContainsNumber ("ABC123")


来源:https://stackoverflow.com/questions/22087675/crystal-reports-check-if-string-contains-any-numerical-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!