Title here
Summary here
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in Wolfram Language.
(* The StringCases function provides the number parsing *)
Needs["StringCases`"]
(* With ToExpression, we can parse floating-point numbers *)
f = ToExpression["1.234"]
Print[f]
(* For parsing integers, we can use ToExpression or IntegerString *)
i = ToExpression["123"]
Print[i]
(* Wolfram Language recognizes hex-formatted numbers *)
d = ToExpression["16^^1c8"]
Print[d]
(* For parsing unsigned integers, we can use FromDigits *)
u = FromDigits["789"]
Print[u]
(* StringCases with NumberString can be used for basic number parsing *)
k = First[StringCases["135", NumberString]]
Print[k]
(* Parsing functions will return $Failed on bad input *)
e = Check[ToExpression["wat"], $Failed]
Print[e]When you run this script, you should see the following output:
1.234
123
456
789
135
$FailedIn Wolfram Language, we use ToExpression for general parsing of numerical strings. For specific types:
ToExpression can handle both floating-point and integer parsing.FromDigits is useful for parsing unsigned integers.StringCases with NumberString can be used for flexible number parsing.Error handling in Wolfram Language is often done using Check, which allows you to specify a value to return if an error occurs.
Next, we’ll look at another common parsing task: URLs.