MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: StringJoin with ToString

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98600] Re: StringJoin with ToString
  • From: replicatorzed at gmail.com
  • Date: Tue, 14 Apr 2009 06:14:24 -0400 (EDT)
  • References: <gr6cbs$pva$1@smc.vnet.net> <gra1qi$2jl$1@smc.vnet.net>

Dear All,

I've considered your warnings, but - mainly because of my curiosity -
I continued to find a solution to the problem. Combining information
from here and my earlier tries, I've managed to come up with a stable
code that is quite short:

toString[expr_String] := expr;
toString[expr_] := ToString[expr];

Unprotect[System`StringJoin];
Attributes[System`StringJoin] = {};
System`StringJoin[expr___] := StringJoin[{expr}];
System`StringJoin[expr_List] := Module[{s = ""}, Scan[(s = StringInsert
[s, toString[#], -1]) &, expr]; s]
Protect[System`StringJoin];

Extensive testing shows that this works as expected (error messages
are returned unchanged, Import[file, "Lines"] works correctly, etc.).
Of course, there are effects, which must be considered, e.g. David's
example:

ss = Quiet[2 <> 3];
Map[ToString[#^2] &, ss]

this indeed would give 23 instead of 49, but that is definitely not
disrupting, since one who uses the new StringJoin *should be aware*
that it actually joins non-strings as well. I wouldn't consider it a
problem, just a point, where it must be kept in mind what the extended
StringJoin can do. To make my point clearer: no one would expect the
following

ss = Quiet[2\[CirclePlus]3];
Map[ToString[#^2] &, ss]

to result other than "4"\[CirclePlus]"9" IFF there is no assigned
meaning of CirclePlus. Although if one uses CirclePlus to actually do
something, then the result of the first line won't be the same as the
input: 2\[CirclePlus]3. As one would expect.

For the other problematic example of Albert (modified here a bit):

If[Head[Quiet[StringJoin[1, 2, 3]]] === StringJoin, Print["this was no
string!"], Print["string ok"]]

This should (in case of the new StringJoin) result "string ok", while
in case of the original StringJoin: "this was no string!". But the
former is exactly the functionality I would like to gain, namely to
get a string from StringJoin even in case of non-string input.

So, of course one must be cautios, but I wouldn't consider these cases
problematic.
Could anyone find any abnormal/unexpected behaviour of this new
function?

Thanks for your help,

Istvan


  • Prev by Date: Re: Help with ScriptSource in GUIKit
  • Next by Date: Re: Importing a large image...
  • Previous by thread: Re: StringJoin with ToString
  • Next by thread: Can nobody help me? Re: selecting a column in a grid