Determine if a string is numeric: Difference between revisions
Content added Content deleted
(Updated D entry) |
|||
Line 391: | Line 391: | ||
<lang d>import std.stdio, std.string, std.conv; |
<lang d>import std.stdio, std.string, std.conv; |
||
bool isNumeric(string s) { |
bool isNumeric(in string s) /*pure*/ { |
||
try |
try |
||
to!real(s.strip()); |
to!real(s.strip()); |
||
Line 402: | Line 402: | ||
foreach (s; ["12", " 12\t", "hello12", "-12", "02", |
foreach (s; ["12", " 12\t", "hello12", "-12", "02", |
||
"0-12", "+12", "1.5"]) |
"0-12", "+12", "1.5"]) |
||
writefln(`isNumeric("%s") = %s`, s, isNumeric(s)); |
|||
writeln("\nNo hex or binary conversion:"); |
writeln("\nNo hex or binary conversion:"); |
||
foreach (s; ["0x10", "6b"]) |
foreach (s; ["0x10", "6b"]) |
||
writefln(`isNumeric("%s") = %s`, s, isNumeric(s)); |
|||
}</lang> |
}</lang> |
||
Output: |
Output: |