to_string
| Definition: | string res = to_string(object data_in, integer string_quote=0, integer embed_string_quote='"') |
| Description: |
Converts an object into a text string.
data_in: Any Phix object. string_quote: If not zero (the default) this is used to enclose data_in, if it is already a string. embed_string_quote: Used to enclose any strings embedded inside data_in. The default is double quote. Returns the string repesentation of data_in. |
| pwa/p2js: | Supported, but only somewhat unofficially/reluctantly, see examples and alternatives below. |
| Comments: |
The returned value is guaranteed to be a displayable text string.
string_quote is only used if data_in is already a string. In this case, if string_quote is zero the string is returned unaltered, otherwise all occurances of string_quote already in data_in are prefixed with the ’\’ escape character, as are any pre-existing escape characters. Then string_quote is added to both ends of data_in, resulting in a quoted string. embed_string_quote is only used if data_in is a sequence that contains strings. In this case, it is used as the enclosing quote for embedded strings. See also (s)printf()’s '%v' handling, which is often easier, and/or ppp. This routine is defined in builtins\to_str.e (an autoinclude) and is provided only for compatibility with Euphoria. |
| Example: |
printf(1,"%s\n",{to_string(12)}) -- 12
printf(1,"%s\n",{to_string("abc")}) -- abc
printf(1,"%s\n",{to_string("abc",'"')}) -- "abc"
printf(1,"%s\n",{to_string(`abc\"`,'`')}) -- `abc\\"` -- see note
printf(1,"%s\n",{to_string({12,"abc",{4.5, -99}})}) -- {12, "abc", {4.5, -99}}
printf(1,"%s\n",{to_string({12,"abc",{4.5, -99}},0,0)}) -- {12, abc, {4.5, -99}}
puts(1,"\n")
printf(1,"%v\n",{12}) -- 12
printf(1,"%v\n",{"abc"}) -- "abc" (no quote-less %v, that'd be %s)
printf(1,"%v\n",{`abc\"`}) -- `abc\"` -- that's better!
printf(1,"%v\n",{{12,"abc",{4.5,-99}}}) -- {12,"abc",{4.5,-99}} -- ("")
puts(1,"\n")
pp(12) -- 12
pp("abc",{pp_StrFmt,-1}) -- abc
pp("abc") -- "abc"
pp(`abc\"`) -- `abc\"` -- ditto
pp({12,"abc",{4.5, -99}}) -- {12, `abc`, {4.5,-99}}
pp({12,"abc",{4.5, -99}},{pp_StrFmt,-1}) -- {12, abc, {4.5,-99}}
Note: I spotted in passing that the fourth item was completely wrong [compared to Euphoria?]. I corrected it best I could but cannot (be bothered to) precisely justify exactly why it behaves in the particular way that it does. Personally I would use and recommend sprint() or the (phix-specific) printf(%v) instead. |