0
$\begingroup$

How can I check if the string contains a real or integer number, space and string?

e.g. StrCheck["156 af"] = true StrCheck["15.6 af"] = true

StrCheck["a 156 af"] = false StrCheck["a 1.56 af"] = false

Thank you!

$\endgroup$
3
  • $\begingroup$ Your examples are not really conclusive. What are the criteria for the decision? $\endgroup$
    – Yves Klett
    Commented Mar 30, 2015 at 15:29
  • 1
    $\begingroup$ StringMatchQ[string,DigitCharacter .. ~~ __]? $\endgroup$ Commented Mar 30, 2015 at 15:34
  • $\begingroup$ StringMatchQ[rhs, DigitCharacter .. ~~ ___ ~~ LetterCharacter] $\endgroup$
    – SuTron
    Commented Mar 30, 2015 at 15:37

1 Answer 1

1
$\begingroup$
f = StringMatchQ[#, NumberString ~~ " " ~~ __] &;

f /@ {"156 af", "15.6 af", "a 156 af", "a 1.56 af"}

{True, True, False, False}

See StringMatchQ, StringExpression, NumberString, BlankSequence, Slot, Function

Related: How can I improve NumberString?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.