String is it Test
From Erlang Community
(Difference between revisions)
| Revision as of 21:49, 3 September 2006 (edit) Bfulgham (Talk | contribs) ← Previous diff |
Current revision (01:11, 10 March 2007) (edit) (undo) Bfulgham (Talk | contribs) (→Solution) |
||
| Line 34: | Line 34: | ||
| 2> end | 2> end | ||
| 2> end. | 2> end. | ||
| - | # | + | #Is_String<erl_eval.6.39074546> |
| 3> A. | 3> A. | ||
| "This is fun!" | "This is fun!" | ||
Current revision
[edit] Testing Whether an Object is a String
[edit] Problem
You need to test if an object is a string. Since Erlang does not have objects per se, the goal here is really that you want to test if a term is a string.
[edit] Solution
You can't! An Erlang string is just a list of characters (really, a list of integer values). So, the variable you are examining could be a list of letters, a set of coordinates, or a set of bytecode for Donald Knuth's MMIX machine.
About the best you can do is determine that the term you are evaluating is a list or not. For that, use the Erlang built-in-function is_list:
1> A="This is fun!". "This is fun!" 2> is_list(A). true 3> B=12. 12 4> is_list(B). false |
If you were very concerned that a term was printable, you could verify that it consists of a list of integers that fall within the range of characters you consider to be a string:
1> Fun = fun(XX) -> 1> if XX < 0 -> false; 1> XX > 255 -> false; 1> true -> true 1> end 1> end. #Fun |

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

