问题
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