Determine if a string is numeric: Difference between revisions
Content added Content deleted
(Updated D entry) |
|||
Line 392: | Line 392: | ||
<lang d>import std.stdio, std.string, std.conv; |
<lang d>import std.stdio, std.string, std.conv; |
||
bool isNumeric(in string s) /*pure*/ { |
bool isNumeric(in string s) /*pure nothrow*/ { |
||
try |
try |
||
to!real(s.strip()); |
to!real(s.strip()); |
||
Line 405: | Line 405: | ||
writefln(`isNumeric("%s") = %s`, s, isNumeric(s)); |
writefln(`isNumeric("%s") = %s`, s, isNumeric(s)); |
||
writeln("\ |
writeln("\nCurrently no hex or binary conversion:"); |
||
foreach (s; ["0x10", "6b"]) |
foreach (s; ["0x10", "6b"]) |
||
writefln(`isNumeric("%s") = %s`, s, isNumeric(s)); |
writefln(`isNumeric("%s") = %s`, s, isNumeric(s)); |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre>isNumeric("12") = true |
<pre>isNumeric("12") = true |
||
isNumeric(" 12 ") = true |
isNumeric(" 12 ") = true |
||
Line 419: | Line 419: | ||
isNumeric("1.5") = true |
isNumeric("1.5") = true |
||
Currently no hex or binary conversion: |
|||
isNumeric("0x10") = false |
isNumeric("0x10") = false |
||
isNumeric("6b") = false |
isNumeric("6b") = false |
||
</pre> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |