Hi everyone
I have a list (and sometimes an array) of strings, some of which are number strings:
listOfStrings = {"Not a number", "12.345", "Not this one either", "-934"};
I'd like to replace the number strings by their corresponding numbers. The fastest way I've found is using a plain old If statement
If[StringMatchQ[#, NumberString],
ToExpression[#], #] & /@ listOfStrings
A more complicated and slower way is to use StringCases and delete the excess baggage in the result:
DeleteCases[
Flatten[StringCases[
listOfStrings, {char : LetterCharacter ..,
num : NumberString} :> {char, ToExpression[num]}]], Null | ""]
Just curious to know if there is a more direct way than either of these.
Gregory